6

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->field_threshold_quantity['und'][0]['value'];
  if(is_numeric($threshold) && $threshold != ''){
    if($quantity >= $threshold){
      $box_code = $product->field_box_code['und'][0]['value'];
      $product->sku = $box_code ;
      $quantity = 1;
    }

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

  // Save new order to get order id
  if(empty($order->order_id)){
    $order->revision = FALSE;
    commerce_order_save($order);
  }
  $order_id = $order->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->uid;
  $line_item = commerce_product_line_item_new($product, $quantity);
  $line_item->field_line_item_audience['und'][]['tid'] = $audience;
  $line_item->field_line_item_item_type['und'][]['tid'] = $material;
  $line_item_added = commerce_cart_product_add($uid, $line_item, FALSE);
  //$order_wrapper->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->revision = FALSE;
  $order_wrapper->save();
  • 1
    Please help me asap. – user23774 Jan 8 '15 at 9:46
  • There a reason you're making the order entity manually instead of using commerce_cart_product_add()? – Ryan Szrama Jan 10 '15 at 13:51
  • Yes, I am still facing the issue.Please suggest something – user23774 Jan 13 '15 at 9:53
  • My suggestion depends on your answer to my question, because I'd just use commerce_card_product_add() if I were you. : ) – Ryan Szrama Jan 14 '15 at 17:41
4

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();
|improve this answer|||||
  • good point, I have this problem now – David Mcsmith Feb 3 '15 at 11:26
  • @DavidMcsmith your welcome :) – Yusef Feb 3 '15 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.