commerce_cart_order_load not working for anonymous user in drupal commerce I have created an order programmatically, the order is created and shown in backoffice order listing, but most of the time in the cart it shows empty.When I checked I found that commerce_cart_order_load for anonymous user return false.

Please help me to find the solution.

Here is the code on submit function i have written :

global $user;
$product_id = $form_state['input']['product_id'];
$quantity = $form_state['input']['quantity'];
$audience = $form_state['input']['audience_id'];
$material = $form_state['input']['material_id'];
if ($product = commerce_product_load($product_id)) {
  $threshold = $product-&gt;field_threshold_quantity['und'][0]['value'];
  if(is_numeric($threshold) &amp;&amp; $threshold != ''){
    if($quantity &gt;= $threshold){
      $box_code = $product-&gt;field_box_code['und'][0]['value'];
      $product-&gt;sku = $box_code ;
      $quantity = 1;
    }

  }
  if (!commerce_cart_order_session_exists())
   {
                $order = commerce_cart_order_new();
               $order-&gt;log = t("Created by checkout page.");
               commerce_cart_order_session_save($order-&gt;order_id);
     }
  // Attempt to load existing order for user from session
   $order = commerce_cart_order_load();
    if(!$order){
      $order = commerce_cart_order_new($user-&gt;uid);
    }

  // Save new order to get order id
  if(empty($order-&gt;order_id)){
    $order-&gt;revision = FALSE;
    commerce_order_save($order);
  }
  $order_id = $order-&gt;order_id;
  // Wrapper for easy field access and adding line items, order passed by reference
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  //uid of the logged in user
  $uid = $user-&gt;uid;
  $line_item = commerce_product_line_item_new($product, $quantity);
  $line_item-&gt;field_line_item_audience['und'][]['tid'] = $audience;
  $line_item-&gt;field_line_item_item_type['und'][]['tid'] = $material;
  $line_item_added = commerce_cart_product_add($uid, $line_item, FALSE);
  //$order_wrapper-&gt;commerce_line_items[] = $line_item;
  commerce_line_item_save($line_item);
  if($uid  == 0 ){
      commerce_cart_order_session_save($order_id);
  }
 //
  // Save new line item to get line item id

 // Add line item to order wrapper
//  

  // Save order with new order number and line item added
  $order-&gt;revision = FALSE;
  $order_wrapper-&gt;save();
Every thing is ok,I had the same problem after spend a half night I found that sometimes drupal dont start session for anonymous users then you should start it manually if begin of your function

   drupal_session_start();