Програмное создание заказа (Commerce Drupal7)

  1. global $user;
  2.  
  3. $product_id = 2;
  4. // Create the new order in checkout; you might also check first to
  5. // see if your user already has an order to use instead of a new one.
  6. $order = commerce_order_new($user->uid, 'checkout_checkout');
  7. // Save the order to get its ID.
  8. commerce_order_save($order);
  9.  
  10. // Link anonymous user session to the cart
  11. if (!$user->uid) {
  12. commerce_cart_order_session_save($order->order_id);
  13. }
  14.  
  15. // Load whatever product represents the item the customer will be
  16. // paying for and create a line item for it.
  17. $product = commerce_product_load($product_id);
  18. $line_item = commerce_product_line_item_new($product, 1, $order->order_id);
  19.  
  20. // Save the line item to get its ID.
  21. commerce_line_item_save($line_item);
  22.  
  23. // Add the line item to the order using fago's rockin' wrapper.
  24. $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  25. $order_wrapper->commerce_line_items[] = $line_item;
  26.  
  27. // Save the order again to update its line item reference field.
  28. commerce_order_save($order);
  29.  
  30. // Redirect to the order's checkout form. Obviously, if this were a
  31. // form submit handler, you'd just set $form_state['redirect'].
  32. drupal_goto('checkout/' . $order->order_id);
Russian
Категория: 
The code has been tested and works
Мультитег: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Target Image