Modifying Commerce Cart Before Display We have a Drupal Commerce cart. The pricing can be altered based on discounts that come in from an external database via XML httprequests. It worked -fine- when the XML requests were made when the user hit the 'Add' button. But another developer removed that code and now wants the XML requests to be made on the cart form.

The problem I'm having is that it appears that the cart wants to display -before- my code will trigger and update the line items.

So... is there a way to hook onto a cart form/view so that my update code fires -before- the view is actually rendered? Below is a snippet. What happens is that the default pricing displays and -then- when the user hits the 'Update Cart' button, the code fires and updates the pricing with the discounts from the external database.

function formxml_form_alter($form, $form_state, $form_id){ 
global $user;
if(! $user)
  return;

if($form_id == 'views_form_commerce_cart_form_default')
      $order = commerce_cart_order_load($user-&gt;uid);

     // request pricing from external site and store in array 
 $request_string = 'http://mysite/Start?CartID=' . $customer_number . '&amp;Cust=' . $customer_number . '&amp;Cntact=' . $contact_number;
    $result = drupal_http_request($request_string, $options);       

$seq = 1;
foreach ($order_wrapper-&gt;commerce_line_items as $line_item_wrapper) {
    $line_item = $line_item_wrapper-&gt;value();
    $my_product_id =  $line_item-&gt;commerce_product[LANGUAGE_NONE][0]['product_id'];
    $product = commerce_product_load($my_product_id);
        $my_product_title = rtrim($product-&gt;title);
            // update each line item with prices from external db array
            ...
     } 
    // now save order
$form_state['order'] = commerce_order_status_update($order, 'cart', TRUE);
commerce_order_save($form_state['order']);
The best and cleanest way to achieve what you want is with a product pricing rule and a custom action with what you have above. That way it goes through the normal Commerce hooks. You just create a rule reacting on "Calculate the sell price of a product."

If you only want the rule to fire on cart and checkout pages, you can also create a custom condition that checks your current path (or there might be a module that adds that condition for you.)

So instead of a hook_form_alter, you want to declare a hook_rules_action_info().