Drupal Commerce: dynamically update line item price

If you want to handle Drupal Commerce line item price programmatically, below code snippet can be helpful for you.

<?php
function MYMODULE_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
                           
$baseprice  = 2500;
                              
$base_price = array(
                   
'amount' => $baseprice,
                   
'currency_code' => 'USD',
                   
'data' => array(),
                  );
               
               
$line_wrapper->commerce_unit_price->data = commerce_price_component_add(
                   
$base_price,
                   
'base_price',
                   
$base_price,
                   
TRUE
               
);
               
$line_wrapper->commerce_unit_price->amount->set($baseprice);
               
$line_wrapper->save();
}
?>

Share this post

Leave a comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.