1

I've setup another commerce price field which I want to use instead of 'default' commerce_price field (under certain conditions). hook_commerce_product_calculate_sell_price_line_item_alter() does the job on full product page but it doesn't work 'globally'. In shopping cart / order page price goes back to default commerce_price field. Actually it's acting strange because when I add product to the shopping cart it shows amended price but it goes back to default price once page is refreshed...

p.s. I can't use Rules to change price.

  • Can we do this in Commerce 2 Drupal 8? – petergus Nov 7 '18 at 3:05
2

Unfortunately using rules is the only option to dynamically alter a product's price in Commerce 1.x. From the 2.0 roadmap:

In Commerce 2.x, we should ensure that our modules define robust internal APIs that are then used by forms, Rules actions, etc. as opposed to embedding functionality into these systems' related callbacks. Most notably, we should seek to make price manipulation and price component management much easier as we open up the sell price calculation process to direct manipulation in module code (as opposed to strictly using Rules) when sell price pre-calculation is disabled.

emphasis mine.

| improve this answer | |
  • 1
    Indeed. You cannot make this sort of change via a hook, but if you must control this change in code, make a custom Rules action that contains all your logic for the swap and then just call that from a pricing rule. See prezi.com/e7mfg8u5qldv/sell-price-calculation for more information on the price calculation process. – Ryan Szrama Jun 22 '13 at 1:00
  • I'm using hook_commerce_product_calculate_sell_price_line_item_alter and hook_commerce_cart_line_item_refresh and it seems to work ok. I've got Domain Access module installed and need to have 3 different prices per product. So I've setup 2 additional price fields and use them instead of commerce_price depending on domain. All works well except for Commerce Pricing Attributes, but i'm about to fix this now... – Lukas C Jul 1 '13 at 10:40
1

For those people who don't want to use rules and hope to alter the price directly. Here is my solution:

//alter the price in list and single product page
function my_module_commerce_product_calculate_sell_price_line_item_alter($line_item){

    $price = 100; //1 dollar
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount']=$price;

}

//alter the price in cart & order
function my_module_commerce_cart_line_item_refresh($line_item, $order_wrapper){

    $price = 100; //1 dollar
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount']=$price;
    $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount']=$price; //alter the base_price component

}
| improve this answer | |
1

I've ended up amending quite few functions to get desired price change. Also use Domain access module. Example code :


function custom_module_commerce_product_calculate_sell_price_line_item_alter($line_item) {
  global $user;

  $oryginal_price = $line_item->commerce_unit_price['und'][0]['amount'];

  $product_id = $line_item->commerce_product['und'][0]['product_id'];

  $website = $_SERVER['SERVER_NAME'];

  if($website == 'website2.local') {
      $val = 5000;
  }
  elseif($website == 'website1.local') {
      $table = 'field_data_field_price_nnd';
      $price_field = 'field_price_nnd_amount';
  }
  else {
      return;
  }

  if($val)
    $line_item->commerce_unit_price['und'][0]['amount'] = $val;
}

type == 'product') {

  $product_id = $line_item->commerce_product['und'][0]['product_id'];   

  $website = $_SERVER['SERVER_NAME'];

  if($website == 'website2.local') {
      $val = 5000;
  }
  elseif($website == 'website1.local') {
      $table = 'field_data_field_price_nnd';
      $price_field = 'field_price_nnd_amount';

  }
  else {
      return;
  }

  if($val) {
    $price = $val;



  if($line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'][1]['price']['amount']) {
      $price = $val + $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'][1]['price']['amount'];
  }

  $qty = $line_item->quantity;
  $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'][0]['price']['amount'] = $price;
  $line_item->commerce_unit_price[LANGUAGE_NONE][0]['amount'] = $price;
  $line_item->commerce_total[LANGUAGE_NONE][0]['data']['components'][0]['price']['amount'] = $qty*$price;
  $line_item->commerce_total[LANGUAGE_NONE][0]['amount'] = $qty*$price;
  commerce_line_item_save($line_item);
  entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id));


   }
 }
}


function custom_module_commerce_product_calculate_sell_price_line_item_alter($line_item) {
  global $user;

  $oryginal_price = $line_item->commerce_unit_price['und'][0]['amount'];

  $product_id = $line_item->commerce_product['und'][0]['product_id'];

  $website = $_SERVER['SERVER_NAME'];

  if($website == 'website2.local') {

      $val = 5000;
  }
  elseif($website == 'website1.local') {
      $table = 'field_data_field_price_nnd';
      $price_field = 'field_price_nnd_amount';
  }
  else {
      return;
  }

  if($val)
    $line_item->commerce_unit_price['und'][0]['amount'] = $val;
  // Reference the current shopping cart order in the line item if it isn't set.

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.