title: Drupal Commerce Line Items: alter the price?
body: <p>I have to add to my cart some line items with a custom amount.
  The commerce product is saved with price = 0, and my module compute the price and add the line item to the cart/order, but i dont understand how to set programmatically the price.</p>
  
  <p>I've read about using Rules, but I need my module to be able to set/alter the price, <strong>without invoking rules</strong>.</p>
  
  <p>I've tryed with an entity wrapper, i tryed to alter the line item created with commerce_product_line_item_new(), but nothing, when the line item gets into the cart always has the original product price (in my case, 0).</p>
  
  <p>How to alter a line item price programmatically?</p>
  
  <p>My code so far looks like:</p>
  
  <pre><code>// For debugging, this function is called by hook_menu()
  function mymodule_test($product_id)
  {
      global $user;
      $user = user_load($user-&gt;uid);
  
      $order = commerce_cart_order_load($user-&gt;uid);
      $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  
      $product = commerce_product_load($product_id);
  
      $line_item = commerce_product_line_item_new(
              $product,
              1,
              0,
              array(
              ),
              'cover'
      );
  
      $line_item_wrapper = entity_metadata_wrapper("commerce_line_item", $line_item);
  
      $line_item_wrapper-&gt;commerce_unit_price-&gt;data = commerce_price_component_add(
              $line_item_wrapper-&gt;commerce_unit_price-&gt;value(),
              'base_price',
              array(
                              'amount' =&gt; 1234,
                              'currency_code' =&gt; 'EUR',
                              'data' =&gt; array(),
              ),
              TRUE
      );
  
      $insert_line_item = commerce_cart_product_add($user-&gt;uid, $line_item_wrapper-&gt;value(), FALSE);
  
      return 'done';
  }
  </code></pre>
  
  <p>The strange thing, is that I tryed to adapt the code of  commerce_line_item_unit_price_amount() found in commerce/modules/line_item/commerce_line_item.rules.inc, but this test:</p>
  
  <pre><code>&lt;?php
      global $user;
      $product = commerce_product_load(4); // my commerce product for test
  
      $line_item = commerce_product_line_item_new(
          $product,
          1,
          0,
          array(
          ),
          'cover' // I do have this line_items type
      );
  
      // manually set amount and component name
      $amount = 1234;
      $component_name = 'base_price'; // tryed with discount, nothing change
  
      $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
  
      // Calculate the updated amount and create a price array representing the
      // difference between it and the current amount.
      $current_amount = $unit_price['amount'];
      $updated_amount = commerce_round(COMMERCE_ROUND_HALF_UP, $amount);
  
      $difference = array(
          'amount' =&gt; $updated_amount - $current_amount, 
          'currency_code' =&gt; $unit_price['currency_code'], 
          'data' =&gt; array(),
      );
  
      // Set the amount of the unit price and add the difference as a component.
      $wrapper-&gt;commerce_unit_price-&gt;amount = $updated_amount;
  
      $wrapper-&gt;commerce_unit_price-&gt;data = commerce_price_component_add(
          $wrapper-&gt;commerce_unit_price-&gt;value(), 
          $component_name, 
          $difference, 
          TRUE
      );
  
      $insert_line_item = commerce_cart_product_add($user-&gt;uid, $line_item, FALSE);
  ?&gt;
  </code></pre>
  
  <p>still fail, the line_item get into the cart but with the original price of the referenced product.</p>
  
  <p>Any idea?</p>