How to change the price ? (add_to_cart) Here is my code


function addToCart($product_id, $quantite){
    global $user;
    $product = commerce_product_load($product_id);
  $line_item = commerce_product_line_item_new($product, $quantite);
  $line_item->data['context']['product_ids'] = array($product_id);
  $line_item->commerce_unit_price->amount  = '900';
  commerce_cart_product_add($user->uid, $line_item);
}


The above code add the product to the cart with the correct quantity but the price isn't changed ...
Any idea ?

Update (resolved thanks to scott's answer)


//Panier
function addToCartCallback()
{
    addToCart($_POST['product_id'], $_POST['quantite']);
}

function addToCart($product_id, $quantite){
  global $user;
  $product = commerce_product_load($product_id);
  $line_item = commerce_product_line_item_new($product, $quantite);
  $line_item->data['context']['product_ids'] = array($product_id);
  commerce_cart_product_add($user->uid, $line_item);
  $order = commerce_cart_order_load($user->uid);
  commerce_cart_order_refresh($order);
}


function groupes_views_commerce_cart_line_item_refresh($line_item, $order_wrapper) {
  global $user;
  $idProduit = $line_item->commerce_product['und'][0]['product_id'];
  $prix = getPrixClient(getIdClient($user->uid), $idProduit) * 100;
  $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['amount'] = $prix;
  $line_item->commerce_unit_price[LANGUAGE_NONE]['0']['data']['components']['0']['price']['amount'] = $prix;
}
I thought of another way to do it.

Start with code that just adds the product to the cart without modifying.

function addToCart($product_id, $quantite) {
    commerce_cart_product_add_by_id($product_id, $quantite);
}


Then you can use a hook to modify the line item amount

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

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

}


If this doesn't work try forcing the cart refresh 
e.g.

function addToCart($product_id, $quantite) {
    global $user;
    commerce_cart_product_add_by_id($product_id, $quantite);
    $order = commerce_cart_order_load($user-&gt;uid);
    commerce_cart_order_refresh($order);
}