Different role prices. Do you think is a good solution?

Posts: 10
Joined: 08/09/2007

Hi everybody!

I need the posibility of havind different prices per role. I've written a few lines, and it seems to work, but I don't know if there's something that I'm forgetting. My hack is done in uc_cart.module.

I've added this simple code in line 1862:

global $user;
if (in_array("Botigues", $user->roles)) {
$item->price = $product->list_price;
}
if (in_array("Empreses", $user->roles)) {
$item->price = $product->cost;
}

and I've made my own template for products, so I display the price similarly as I change it in the code above. Do you think that I can have any problem doing this?

Thank you very much!

Posts: 10
Joined: 08/09/2007

no one? is it terrible?

Posts: 110
Joined: 05/28/2008

Wow. I have been looking for this exact thing! It seems like a workable solution, but its not very Drupal-y (e.g. upgrade friendly).

What function did you make the code change in? Is it possible to override that function in your Template.php?

Have you tested this through checkout and does the price stay consistent?

Thanks for posting this!

Posts: 10
Joined: 08/09/2007

I'm glad I've helped someone. I know this is not a good solution for the upgrades but... I haven't found any other solution.
As far as I'm concerned, I think that the function is not themeable (uc_cart_get_contents).
I've made few tests... If you have any problem with it and post it here, you would give me back the help Eye-wink

Posts: 5
Joined: 08/07/2008

This seems useful, however I'm having problems with this code - I put it in after

$product = node_load($item->nid);
      $item->cost = $product->cost;
      $item->price = $product->sell_price;
      $item->weight = $product->weight;

but just get a white screen - I think it's $global user; causing the problem so I took it out but then I get the error 'warning: in_array(): Wrong datatype for second argument. Did I put it in the wrong place? (sorry don't have line numbers in my text editor).

I'm also looking to do a similar thing for attributes (using attribute cost instead of sell price for certain role), so any help would be appreciated!
Thanks
John

Posts: 5
Joined: 08/07/2008

Ok I think I figured it out - supposed to be global $user, right?

Posts: 5617
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Ahh, yeah. I'll fix the code in the first post so it doesn't throw anyone else off.

Posts: 5
Joined: 08/07/2008

Ok I've made some more changes to this:
Line numbers are accumulative, based on lines already inserted. Lines also replace any lines mentioned in the code.

***uc_product.module***
Changes made to display the cost price for certain role (in this case "retailer") on product view (but not on teasers, also disables Ajax Attribute Calculations functionality)

Line 735:

global $user;
if (in_array("retailer", $user->roles)){
  $node->content['display_price'] = array('#value' => theme('uc_product_display_price', $node->cost),
    '#access' => $enabled['display_price'],
    '#weight' => $weight['display_price'],
  );
} else {
  $node->content['display_price'] = array('#value' => theme('uc_product_display_price', $node->sell_price),
    '#access' => $enabled['display_price'],
    '#weight' => $weight['display_price'],
  );
}

Line 766:

if (in_array("retailer", $user->roles)){
  $node->content['sell_price'] = array('#value' => theme('uc_product_sell_price', $node->cost, $teaser),
    '#access' => $enabled['sell_price'],
    '#weight' => $weight['sell_price'],
  );
} else {
  $node->content['sell_price'] = array('#value' => theme('uc_product_sell_price', $node->sell_price, $teaser),
    '#access' => $enabled['sell_price'],
    '#weight' => $weight['sell_price'],
  );
}

***uc_attribute.module***
Applies attribute cost instead of price for user role ("retailer" again). You can set the cost in node>edit>options.

Line 315:

      foreach ($item->options as $option) {
        $op_costs += $option['cost'];
        $op_prices += $option['price'];
        $op_weight += $option['weight'];
      }
      $item->cost += $op_costs;
      global $user;
      if (in_array("retailer", $user->roles)){
         $item->price += $op_costs;
      }else{
         $item->price += $op_prices;
      }
      $item->weight += $op_weight;

Line 1476:

// Build the attribute's options array.
      global $user;
      $options = array();

Line 1481:

case 'total':
if (in_array("retailer", $user->roles)){
   $display_price = in_array($relation->aid, $priced_attributes) ? ', '. uc_currency_format(($node->sell_price + $option->cost) * $qty) : '';
} else {
   $display_price = in_array($relation->aid, $priced_attributes) ? ', '. uc_currency_format(($node->sell_price + $option->price) * $qty) : '';
}

and finally 1490:

case 'adjustment':
if (in_array("retailer", $user->roles)){
   $display_price = ($option->cost != 0 ? ', '. ($option->cost > 0 ? '+' : '') . uc_currency_format($option->cost * $qty) : '');
} else {
   $display_price = ($option->price != 0 ? ', '. ($option->price > 0 ? '+' : '') . uc_currency_format($option->price * $qty) : '');
}

I think that's all. An ugly hack but I hope this helps someone as much as it will me. I'll post any issues I get with this as I test it a bit more. Any questions or issues post here cheers!
John

Posts: 1
Joined: 08/27/2008

i wonder why this is not part of the core!
i used your code and it seemed to work fine!
many thanks

Posts: 14
Joined: 04/07/2008
Internationalizationizer

and to add the price into the cart:

* -- uc_cart.module -- *
around lin1889
global $user;
if (in_array("privatkunde", $user->roles)){
$item->price = $product->cost;
}else{
$item->price = $product->sell_price;
}

but this is still pure evil - this should be put out into a module, so we dont end up with a osCommerce way of doing things, hmm "somebody" should do something
and to make this really good it would be sweet if it was based on an cck field etc

/morten.dk

Posts: 2
Joined: 09/11/2008

Hi, I've followed this discussion applying phethean's and mortendk's changes: it is exactly the solution I'm looking for. I've submitted two patches opening this feature request on Drupal.org using an additional function that I hope makes this changes easier centralizing user role control. Any suggestion / modification is welcome: I've not added uc_attribute changes yet and a "discount" role selection by admin GUI is desirable. Please reply to drupal.org issue if interested.

Posts: 9
Joined: 08/22/2008

Would the Discount module not work better?
http://www.ubercart.org/contrib/143

Cheers,

Posts: 56
Joined: 07/16/2008
Getting busy with the Ubercode.

I require this feature as well. Subscribing to this to see what happens.

Role based prices is definately a worthy module for ubercart as it allows retailers to have a different price...I assume this is why we're all here Laughing out loud

Posts: 208
Joined: 12/28/2007
Uber DonorBug Finder

Yes, different prices per roles opens a lot of possibilities. I'd love to see that happen in Ubercart!

Posts: 56
Joined: 07/16/2008
Getting busy with the Ubercode.

Here's a snippet which you can add into one of your modules to avoid having to hack uc_cart.module.

function yourmodule_cart_item($op, &$item) {
   global $user;
   switch($op) {
      case 'load':
         if(in_array('reseller', $user->roles)) {
            // Change the price to what ever in here
            $item->price = $item->cost;
         }
         break;
   }
}

It uses this hook:
http://api.ubercart.org/api/function/hook_cart_item/1.0

If I need a more robust solution, I might go ahead and create a module around this.

Posts: 56
Joined: 07/16/2008
Getting busy with the Ubercode.

It's snippet day from me today. Again, put this in your module. This snippet will display the "cost" on the product page to the "reseller".

You're going to have to enable the "Cost" product field in the admin as well.

http://yoursite.tld/admin/store/settings/products/edit/fields

function yourmodule_nodeapi($node, $op, $arg1 = NULL, $arg2 = NULL) {
   global $user;
   switch($op) {
      case 'view':
         if(in_array($node->type, uc_product_product_types())) {
            if(in_array('reseller', $user->roles)) {
               //dpm(array("WE GOT A RESELLER, SHOW COST" => $node->content['cost']));
               $enabled = variable_get('uc_product_field_enabled', array('cost' => 0));
               $node->content['cost']['#access'] = $enabled['cost'];
            }
         }
      break;
   }
}

Since 'cost' isn't displayed to anyone except for roles with "administer products", this code essentially enable this functionality for your reseller with out the need of having to give their role that permission.

---
<3 the cart (heart the cart)

Posts: 110
Joined: 01/11/2008

Maybe we should set up a bounty to get this coded as a plugin or if possible a feature that can be implemented into UC.

Posts: 123
Joined: 08/07/2007
Bug FinderGetting busy with the Ubercode.

There are a couple things to consider here.

1. Last I checked, the catalog module does not use the nodeapi, which makes it difficult to modify the display price based on the users role.
2. There is currently a uc_discounts module on drupal.org. Although the current workflow would make it very time consuming to offer a discount for every one of your products, for one or more roles. It may be worth your while to create an additional discount plugin that merges the work here, with the discount system for the final cost adjustment.

Posts: 14
Joined: 05/28/2008
Bug Finder

I am currently weighing the best way to do this. One possibility that may be obvious but is not mentioned here is to set up multiple catalogs (multiple taxonomy terms) and display different catalogs to different people according to roles, perhaps using http://drupal.org/project/taxonomy_role.

This would work better if perhaps the catalogs have some different contents in them. You would of course need to maintain more than one "copy" of your product in order to give it a different price, but on the other hand there may be other aspects of the product besides price that you want to differentiate as well (e.g. different wording in the description, different shipping options, etc.).

On the report end, it would show up as separate products, but on some cases this could be a simplification for reporting--it can get complicated when the same product has different prices.

An advantage of this method is of course that you would not need to implement any custom code, or, *gasp*, hack the modules.

Posts: 110
Joined: 01/11/2008

Seems like I'm gonna have to go both retail and wholesale for the last site I build which sucks as atm dual pricing seems like such a hack in UC.

The dual catalog way seemed like a way to do this but I would also have to create dual blocks that display items and have different ones shown per role which seems like a huge redundant way of doing things.

Any more ideas on this would be appreciated.

Arek

hinrichsislcocom wrote:
I am currently weighing the best way to do this. One possibility that may be obvious but is not mentioned here is to set up multiple catalogs (multiple taxonomy terms) and display different catalogs to different people according to roles, perhaps using http://drupal.org/project/taxonomy_role.

This would work better if perhaps the catalogs have some different contents in them. You would of course need to maintain more than one "copy" of your product in order to give it a different price, but on the other hand there may be other aspects of the product besides price that you want to differentiate as well (e.g. different wording in the description, different shipping options, etc.).

On the report end, it would show up as separate products, but on some cases this could be a simplification for reporting--it can get complicated when the same product has different prices.

An advantage of this method is of course that you would not need to implement any custom code, or, *gasp*, hack the modules.