Advertisement
edutrul

Create a drupal commerce order programmatically

Aug 1st, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. global $user;
  2. $product_ids = array(2);
  3. $order = commerce_order_new ( $user->uid , 'pending' );
  4.  
  5.  
  6.     // Save the order so its ID is assigned.
  7.     commerce_order_save ( $order );
  8.  
  9.     $order_wrapper = entity_metadata_wrapper ( 'commerce_order' , $order );
  10.  
  11.     //add products to order by ids array
  12.     foreach ( $product_ids as $product_id ) {
  13.         $product = commerce_product_load ( $product_id );
  14.  
  15.         // Create a line item with quantity 1 and this product.
  16.         $line_item = commerce_product_line_item_new ( $product , 1 , $order->order_id );
  17.  
  18.         // Save the line item to get its ID.
  19.         commerce_line_item_save ( $line_item );
  20.  
  21.         // Add the line item to the order using the wrapper.
  22.         $order_wrapper->commerce_line_items[ ] = $line_item;
  23.     }
  24.  
  25.  
  26.     $profile_object = array (
  27.         'und' => array ( array ( 'profile_id' => 1 , ) , ) , );
  28.    
  29.    
  30.     $order->commerce_customer_billing = $profile_object;
  31.  
  32.     // Save the order.
  33.     commerce_order_save ( $order );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement