Discussions

How to alter the line-item Price programmatically

Hi there

How can i alter the the line-item Price programmatically?

is there something easy like

commerce_alter_line_item_price($line_item,array('add',200));

Posted: Aug 31, 2012

Comments

einsk on August 31, 2012

i found this

$product = $product = commerce_product_load(7);
$product->commerce_price['und'][0]['amount'] = 39900;
commerce_product_save( $product );

everything is fine. the price for the product is changed
the line items price was updated
also in checkpout and review

but if only want to change the line item price

$line_item = commerce_line_item_load(array(4), array());

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

commerce_line_item_save($line_item);

this did not effect the review line items

what is my mistake?

aaronbauman on January 16, 2013

Seriously, why is it so hard to add an item to the cart with a specific price.
I've tried like 6 different approaches, but nothing seems to have any effect.

mikeejt on January 25, 2013

Take a look at:

~/sites/all/modules/contrib/commerce/modules/product_pricing/commerce_product_pricing.api.php

You'll see the hook which you can implement in your module:

hook_commerce_product_calculate_sell_price_line_item_alter($line_item)

fossie on February 3, 2013

Hi,
I also tried to use this hook, but it doesn't effect the line item price, what amount should I change?

<?php
function myticket_commerce_product_calculate_sell_price_line_item_alter($line_item) {
  global
$user;
 
 
// Reference the current shopping cart order in the line item if it isn't set.
 
if (empty($line_item->order_id)) {
   
$line_item->order_id = commerce_cart_order_id($user->uid);
  } 
 
$line_item->commerce_unit_price['und'][0]['amount'] = 10000;
 
$line_item->commerce_unit_price['und'][0]['data']['components'][0]['price']['amount'] = 10000;
 
dpm($line_item);  
 
}
?>

The output of dpm shows the changed amounts, but the item added to the cart still has the original price.

Which value should we change to take effect?

Thank you,
Fossie