11 replies [Last post]
glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Was this information Helpful?

Hi

I struggled long and hard with the uc_custom_price module, and couldn't get it to do what I wanted. I'm trying to write my own module now to achieve what I want.

I need to alter the price of a product depending on:

  1. The Attributes of the product being bought;
  2. The role of the purchaser.

My code is:

<?php

function role_price_cart_item($op, &$item) {
  switch (
$op) {
  case
'load':
    global
$user;
    if (
'[Course Modules]' == 'All modules') {
     
$code = "$item->price = '1553'";   
      }

    if (

'[Course Modules]' == 'Software module') {
     
$code = "$item->price = '816'";
      }

    if (

'[Course Modules]' == 'Theory module') {
     
$code = "$item->price = '628'";
      }

    if (

'[Course Modules]' == 'Professional Practice module') {
     
$code = "$item->price = '298'";
      }

    if (

in_array('member', $user->roles) && '[Course Modules]' == 'All modules') {
     
$code = "$item->price = '1352'";
      }

    if (

in_array('member', $user->roles) && '[Course Modules]' == 'Software module') {
     
$code = "$item->price = '717'";
      }

    if (

in_array('member', $user->roles) && '[Course Modules]' == 'Theory module') {
     
$code = "$item->price = '548'";
      }

    if (

in_array('member', $user->roles) && '[Course Modules]' == 'Professional Practice module') {
     
$code = "$item->price = '253'";
      }

    if (

in_array('assessor', $user->roles) && '[Course Modules]' == 'All modules') {
     
$code = "$item->price = '1091'";
      }

    if (

in_array('assessor', $user->roles) && '[Course Modules]' == 'Software module') {
     
$code = "$item->price = '627'";
      }

    if (

in_array('assessor', $user->roles) && '[Course Modules]' == 'Theory module') {
     
$code = "$item->price = '480'";
      }

    if (

in_array('assessor', $user->roles) && '[Course Modules]' == 'Professional Practice module') {
     
$code = "$item->price = '0'";
      }
     
 
$product = node_load($item->nid);
 
$eval_code = token_replace_multiple($code, array('node' => $product, 'uc_cart_item' => $item));
  eval(
$eval_code);
  break;
  }
}
?>

This is having no effect at all on the price. Where is my error? Do I need to define permissions to be able to use this, or since I have none, does this automatically make it available for everyone?

Thanks

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: New role discount Module

bump

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 850
glennnz: A line like

glennnz: A line like

<?php
if ('[Course Modules]' == 'All modules') {
?>

Is never going to evaluate to true as it is simply comparing two different strings. If you want an attribute of the item, you should print_r($item) and see where that attribute exists instead of trying to use a token, which is this case is not going to work for you.

Oh, and there is no reason you need to set $code and then eval($code), just set $item->price directly.

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
@cYu Thanks. Where do I

@cYu

Thanks. Where do I need to put print_r($item)?

I put it in my product node, but it isn't displaying anything at all.

Thanks

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: @cYu Thanks. Where do I

OK, found the attribute values I need.

I now have:

<?php
function role_price_cart_item($op, &$item) {
  switch (
$op) {
  case
'load':
    global
$user;
    if (
$item->data['attributes'][1] == '1') {
     
$item->price = '1553';   
      }

    if (

$item->data['attributes'][1] == '2') {
     
$item->price = '816';
      }

    if (

$item->data['attributes'][1] == '3') {
     
$item->price = '628';
      }

    if (

$item->data['attributes'][1] == '4') {
     
$item->price = '298';
      }

    if (

in_array('member', $user->roles) && $item->data['attributes'][1] == '1') {
     
$item->price = '1352';
      }

    if (

in_array('member', $user->roles) && $item->data['attributes'][1] == '2') {
     
$item->price = '717';
      }

    if (

in_array('member', $user->roles) && $item->data['attributes'][1] == '3') {
     
$item->price = '548';
      }

    if (

in_array('member', $user->roles) && $item->data['attributes'][1] == '4') {
     
$item->price = '253';
      }
  break;
  }
}
?>

This works perfectly, but ONLY FOR THE ADMINISTRATOR!!

For any other user this has no effect at all on $item->price. This seems to indicate that it is a permissions problem. I am fast running out of time for this to be working, is anyone able to suggest where the problem is?

Thanks heaps

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
UPDATE Further testing,

UPDATE

Further testing, using this code:

<?php

function role_price_perm() {
  return array(
'access role prices');
}

function

role_price_cart_item($op, &$item) {
  if(
user_access('access role prices')) {
    echo
"User has permission";
  }
  else {
      echo
"No permission";
  }
}
?>

"User has permission" shows on the top of EVERY page when products are in the cart.

This message is only displayed once a user adds a product to the cart, which is what I would expect.

This message works fine, but as soon as I put in price alterations, it only works for the administrator. Is there something in the hook_cart_item to do with permissions that I don't know about??

Thanks

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Re: UPDATE Further testing,

Bump, running out of time, help needed.

Thanks heaps.

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
SOLVED There was nothing

SOLVED

There was nothing wrong with my code, another module was rewriting $item->price after mine was...

glenn

LoveGolf's picture
Offline
Joined: 10/16/2008
Juice: 106
Re: SOLVED There was nothing

Which module? Thanks

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
I can't find it now.... It

I can't find it now....

It was a module which modified the display prices to show them VAT/GST inclusive.

Glenn

LoveGolf's picture
Offline
Joined: 10/16/2008
Juice: 106
Re: I can't find it now.... It

Thanks, I saw over in the Drupal forums it was a tax module of some sorts.

BTW ... this code is changing the price of the options inside a attribute, right?

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
If you tell it to, yes Glenn

If you tell it to, yes

Glenn