Discussions

How to change the product price according to user role

Hello,
I have two types of customers: normal customer and dealer
The normal customer by (1 t-shirt * quantity)
The dealer have a discounts. So he can buy packages of t-shirts (2 small, 2 medium and 1 large) with a special price..
So I used the price field to store the normal customer price and used the cost-price to store the package price.
So when checking out.. if the user is an anonymous or normal customer use the amount in "price" field to check out.
Else if the user has the role "dealer" then use the price in the "cost-price" field for checking out.
Better way is to provide dealer with a select list on the "add to cart form" so he can choose to bay an individual product (buy like a normal customer) or buy a "package"..
I hope you discuss the best way to do that and how to do that.
It's really an important feature in most stores to allow "dealers" to by packages instead of individual products.
Actually I don't if this has been done before.
Want your help, thank you.

Posted: May 3, 2012

Comments

wisamx on May 11, 2012

I tried to add a new rule action:
this action override the commerce_price using field_dealer_price

but it doesn't seem to work like i want:

/**
* Implements hook_rules_action_info().
*/
function commerce_dealer_price_rules_action_info() {
$actions = array();

$actions['commerce_set_dealer_price'] = array(
'label' => t('Replace the price for a dealer price.'),
'parameter' => array(
'commerce_line_item' => array(
'type' => 'commerce_line_item',
'label' => t('Line item'),
),
),
'group' => t('Dealer price'),
);

return $actions;
}

function commerce_set_dealer_price($line_item) {
// If the line item contains a product, we set the price according to the quantity.
if (commerce_line_items_quantity(array($line_item), array('product')) > 0) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$product = commerce_product_load($line_item_wrapper->commerce_product->product_id->value());
$product_wrapper = entity_metadata_wrapper('commerce_product', $product);

//Get Product Price From the custom price field 'field_dealer_price'
$dealer_price = $product_wrapper->field_dealer_price->value();

if (!empty($product->field_dealer_price)) {
$line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] = array();

$new_price = array(
'amount' => $dealer_price['amount'],
'currency_code' => $dealer_price['currency_code'],
'data' => array(),
);

// Alter the base price to the new one.
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
$line_item_wrapper->commerce_unit_price->value(),
'base_price',
$new_price,
TRUE
);
}
}
}

DaveP on June 14, 2012

Let me preface this by saying that I am new to Drupal Commerce myself but here's the route I would go. First I would do a hook_entity_property_info_alter to register a new entity that can be used by the rule action, something similar to

$info['commerce_line_item']['bundles']['customshirts']['properties']['dealer_price'] = array(
'label' => t('Dealer Price'),
'type' => 'decimal',
'description' => t('Price for dealers'),
'getter callback' => 'site_apparel_line_item_properties',
);

Then you could do a site_apparel_line_item_properties function to pull back the field value, like.

switch ($name) {
case 'dealer_price':
return commerce_currency_decimal_to_amount(variable_get('variable_where_youre_storing_dealer_price', 8), 'USD');
break;

Now when you create your rule you can set use an event of 'calculate the sell price of a product", for the condition you can use a "user has role(s)". Then in the action you can "set unit price to a certain value" and in the data selector for the value you should see your new commerce-line-item:dealer-price

I haven't had a chance to test it out but in theory I think it should work.