Can I modify prices of base products and attributes based on a user's role?

Thanks

Glenn

Comments

abramo’s picture

Assigned: mrfelton » Unassigned
Status: Postponed (maintainer needs more info) » Active

Yes : example:

<?php
global $user;
if (in_array('Some Role', $user->roles))
{
$item->price = [cost]+10.00;
}
?>

or

<?php
global $user;
if (in_array('Some Role', $user->roles))
{
$item->price = $item->price+10.00;
}
?>

hope this helps.

glennnz’s picture

Hi

I can't get this to work...

Below is what I'm using to test.

The module works fine if I use just:

$item->price = $item->price*3;

But when I use

global $user;
if (in_array('member', $user->roles))
{
$item->price = $item->price*3;
}

the price doesn't change for a user with that role...

Thanks

glennnz’s picture

Also, is there a way to make the displayed price on the product node change?

At the moment, the price is only being modified when the product is added to the cart.

Thanks

glennnz’s picture

This is what I really want to do:

global $user;
if (in_array('member', $user->roles) && '[modules]' == 'theory only')
{
$item->price = '1500';
}
if (in_array('member', $user->roles) && '[modules]' == 'software only')
{
$item->price = '1850';
}

Is this correct?

Also, should the attribute tokens be '[attribute]' or ['attribute']?

Thanks

Glenn

mrfelton’s picture

I don't think you can not use this module to alter the display price. The price modification only affects the price at checkout and it is your responsibility to inform the user if and when discounts/fees may be applied, probably in the product's description. Perhaps you could try the uc_discounts module instead (http://drupal.org/project/uc_discounts), although I'm not really sure if price adjustments are displayed to the user with that module either.

Regarding the token - as far as I know there should only be quote marks if your token holds a string. For example,

[cost] = 4. Then:

// probably the comparison you want  (4 == 4)
[cost] == 4

// probably not the comparison you want ('4' == 4)
'[cost]' == 4

[cost] = 'string'. Then:

// probably the comparison you want ('string' == 'string')
'[cost]' == 'string'

// will probably throw an error (string == 'string')
[cost] == 'string'     

Hope that makes sense.

glennnz’s picture

Great, thanks

The if statement still doesn't work though...

global $user;
if (in_array('me...........
mrfelton’s picture

use the devel module to inspect your array? Perhaps it isn't what you think it is?

glennnz’s picture

Ummmm.... I downloaded and installed the devel module, but can't see the user array.

I tried testing this previously by using:

global $user;
if (in_array('member', $user->roles)) {
  foreach ($user->roles as $row) {
    echo $row . "<br/>";
    }
  }

and this worked fine. It's just when I put the $item->price = $item->price*3; in there that it doesn't work...

I desperately need to get this sorted out, I can call you if you are happy for me to? Email me if this is so.

Thanks

Glenn

glennnz’s picture

OK, still not working, some more info.

With this code:

if ('[Course Modules]' == 'All modules')
{
$item->price = '1';
}

if ('[Course Modules]' == 'Software module')
{
$item->price = '2';
}

if ('[Course Modules]' == 'Theory module')
{
$item->price = '3';
}

if ('[Course Modules]' == 'Professional Practice module')
{
$item->price = '4';
}

it works perfectly for the administrator, but not for anyone else....

HELP!!!!!!!

Thanks

Glenn

mrfelton’s picture

Hi Glenn,
Sorry I didn't respond for so long. My computer died and I had to get an entire new development environment set before I could continue with this stuff. Are you still having trouble with this?

mrfelton’s picture

Assigned: Unassigned » mrfelton
Status: Active » Postponed (maintainer needs more info)
glennnz’s picture

Hi

Yes, have played a little, with no luck at all.

My test code (which is not working at all) is:

global $user;



if ('[Course Modules]' == 'All modules')
{
$item->price = '1553';
}

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

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

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

if ('[Course Modules]' == 'All modules' && '[Course Software]' == 'BERS Pro')
{
$item->price = '1608';
}

if ('[Course Modules]' == 'Software Module' && '[Course Software]' == 'BERS Pro')
{
$item->price = '869';
}




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

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

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

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

if (in_array('absa_member', $user->roles) && '[Course Modules]' == 'All modules' && '[Course Software]' == 'BERS Pro')
{
$item->price = '1403';
}

if (in_array('absa_member', $user->roles) && '[Course Modules]' == 'Software Module' && '[Course Software]' == 'BERS Pro')
{
$item->price = '768';
}




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

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

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

if (in_array('assessor', $user->roles) && '[Course Modules]' == 'Professional Practice module')
{
$item->price = '0';
}

if (in_array('assessor', $user->roles) && '[Course Modules]' == 'All modules' && '[Course Software]' == 'BERS Pro')
{
$item->price = '1142';
}

if (in_array('assessor', $user->roles) && '[Course Modules]' == 'Software Module' && '[Course Software]' == 'BERS Pro')
{
$item->price = '677';
}

Thanks

Glenn

mrfelton’s picture

Your very first example works absolutely fine for me... In the Custom Price Calculation textarea I enter:

global $user;
if (in_array('authenticated user', $user->roles)) {
  $item->price = $item->price+1000.00;
}

When I add the product to my cart, the price shows £1000 more expensive that it was listed in the product page.

Unfortunately I do not have a Drupal 5 setup at the moment, but it should be working in exactly the same way and there are currently a handful of people using it on Drupal 5 sites successfully. When I ported this module to Drupal 6 I did make it clear that I wouldn't be supporting the Drupal 5 version so if you need more help with this, cYu is going to be your man as he is the original author and maintainer of the Drupal 5 version of this module. Alternatively, if you upgrade to Drupal 6 and still have the problem I will be more than willing to peruse this with you.

glennnz’s picture

Assigned: Unassigned » mrfelton
Status: Active » Postponed (maintainer needs more info)

Very strange

Your code works fine for some users, not for others. I'll chat with cYu. Thanks.

Prodigy’s picture

Perhaps some of you are trying for Drupal 5 and some for Drupal 6?

Scott M. Sanders’s picture

This is (almost) exactly what I need for distributor pricing, thanks!

Prodigy’s picture

Using 6 or 5 Scott? And which solution worked for you?

Scott M. Sanders’s picture

d6 -- and actually it almost works, except that I will need to recalculate all of the product prices based on the final order subtotal and against a fixed three-column, tiered distributor pricing sheet that I was given. It's close though. :)

Prodigy’s picture

I'm trying to do something really similar and am curious if it can be accomplished through use of this module.

I need to change the price of 4 different attributes based on the users role.

Below is the code for changing the base price ... may I get some assistance in how to change attributes prices?

<?php
global $user;
if (in_array('authenticated user', $user->roles)) {
  $item->price = $item->price+1000.00;
}
?>
glennnz’s picture

@Prodigy

Here is the code snippet for mine:

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

You just need to figure out the values of your attributes. In this case it was Attribute 1, Option 4.

Cheers

Glenn

Prodigy’s picture

Thanks Glenn! Linking the two as the 2 issues are near in the same http://drupal.org/node/366525

DrPhunk’s picture

Can anyone suggest how one might pull other node fields into this equation? We want multiple set prices for each product that aren't based off a common percentage or amount added / subtracted. What I would like to do is pull in a CCK field value and set it as the price with a fallback to the retail price, e.g.

global $user;
if (in_array('cust_commercial', $user->roles)) {
	if($node->field_prod_price_commercial[0]['value']){
		$item->price = $node->field_prod_price_commercial[0]['value'];
	} else { $item->price = $node->field_prod_price_retail[0]['value']; }
}
elseif (in_array('cust_corporate', $user->roles)) {
	if($node->field_prod_price_corporate[0]['value']){
		$item->price = $node->field_prod_price_corporate[0]['value'];
	} else { $item->price = $node->field_prod_price_retail[0]['value']; }
}
else { $item->price = $node->field_prod_price_retail[0]['value']; }

This doesn't work for me, can anyone offer some help?

Edit - duh, needed to load the node data :P Is there a more efficient way than loading the entire node data set?

global $user;
$node = node_load($item->nid);
if (in_array('cust_corporate', $user->roles)) {
 if ($node->field_prod_price_corporate[0]['value']){
  $item->price = $node->field_prod_price_corporate[0]['value'];
  } else { $item->price = $node->field_prod_price_retail[0]['value']; }
} elseif (in_array('cust_commercial', $user->roles)) {
	if ($node->field_prod_price_commercial[0]['value']){
		$item->price = $node->field_prod_price_commercial[0]['value'];
	} else { $item->price = $node->field_prod_price_retail[0]['value']; }
else { $item->price = $node->field_prod_price_retail[0]['value']; }
mrfelton’s picture

I think node_load is the most efficient way of loading an entire node, since it benefits from static caching. I don't know of a better way. If you only wanted specific bits of data, it may be better do do a few manual database queries, however since you are pulling data from cck fields, and those can be stored in a number of different ways, node_load is probably your best bet

TR’s picture

Status: Postponed (maintainer needs more info) » Fixed

I'm going to marked this as "fixed" and close the issue. This seems to have become a catch-all issue, and as far as I can tell most if not all the various unrelated questions have been answered. If there are still outstanding problems, please open a new issue for your question.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.