Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating new order in Woocommerce

I am having the hardest time programmatically creating an order in WooCommerce. I am using the code below and is DOES create an order BUT I cannot get customer information OR product line items added to the order. The new order that is create is simply as Guest with no items, user information, etc.

The issue seems to be that once the order object is created, it is failing when trying to add data to the order.

function create_vip_order() {    global $woocommerce;    $address = array(       'first_name' => '111Joe',       'last_name'  => 'Conlin',       'company'    => 'Speed Society',       'email'      => '[email protected]',       'phone'      => '760-555-1212',       'address_1'  => '123 Main st.',       'address_2'  => '104',       'city'       => 'San Diego',       'state'      => 'Ca',       'postcode'   => '92121',       'country'    => 'US'   );    // Now we create the order   $order = wc_create_order();    // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php   $order->add_product( get_product( '275962' ), 1 ); // This is an existing SIMPLE product   $order->set_address( $address, 'billing' );   //   $order->calculate_totals();   $order->update_status("Completed", 'Imported order', TRUE);  }  add_action( 'woocommerce_init', 'create_vip_order' ); 

Here is the error I am getting in my logs:

[19-Apr-2016 21:16:38 UTC] PHP Fatal error:  Uncaught Error: Call to a member function add_product() on boolean in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php:107 Stack trace: #0 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): create_vip_order('') #1 /Users/joe/Sites/speedsociety-2/wp-content/plugins/woocommerce/woocommerce.php(330): do_action('woocommerce_ini...') #2 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): WooCommerce->init('') #3 /Users/joe/Sites/speedsociety-2/wp-settings.php(392): do_action('init') #4 /Users/joe/Sites/speedsociety-2/wp-config.php(67): require_once('/Users/joe/Site...') #5 /Users/joe/Sites/speedsociety-2/wp-load.php(37): require_once('/Users/joe/Site...') #6 /Users/joe/Sites/speedsociety-2/wp-admin/admin.php(31): require_once('/Users/joe/Site...') #7 /Users/joe/Sites/speedsociety-2/wp-admin/edit.php(10): require_once('/Users/joe/Site...') #8 {main}   thrown in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php on line 107 

Any help on this would be MOST appreciated!

like image 634
Joe Conlin Avatar asked Apr 19 '16 21:04

Joe Conlin


People also ask

How do I create a custom order in WooCommerce programmatically?

function create_vip_order() { global $woocommerce; $address = array( 'first_name' => '111Joe', 'last_name' => 'Conlin', 'company' => 'Speed Society', 'email' => '[email protected]', 'phone' => '760-555-1212', 'address_1' => '123 Main st.

How do I make all orders in WooCommerce programmatically?

Try This Code. $args = array( 'post_type' => 'shop_order', 'posts_per_page' => '-1' ); $my_query = new WP_Query($args); $orders = $my_query->posts; echo "</pre>"; print_r($orders); echo "<pre>"; Customize As par Ur Requerment.

How do I add a product in WooCommerce programmatically?

Add variable products programmatically To create variable products programmatically in WooCommerce, you just need to change the second parameter of the wp_set_object_terms() function: wp_set_object_terms( $post_id, 'variable', 'product_type' ); Similarly, you can define a product as grouped or external.

How do I automatically change the order status in WooCommerce?

Setup. To setup Order Status Control, go to WooCommerce > Settings > General and update the Orders to Auto-Complete setting to determine which paid orders should skip the Processing status and go directly to Completed status: None: No orders will be automatically completed.


1 Answers

The problem is in your action hook. Use following hook :

add_action('woocommerce_checkout_process', 'create_vip_order');  function create_vip_order() {    global $woocommerce;    $address = array(       'first_name' => '111Joe',       'last_name'  => 'Conlin',       'company'    => 'Speed Society',       'email'      => '[email protected]',       'phone'      => '760-555-1212',       'address_1'  => '123 Main st.',       'address_2'  => '104',       'city'       => 'San Diego',       'state'      => 'Ca',       'postcode'   => '92121',       'country'    => 'US'   );    // Now we create the order   $order = wc_create_order();    // The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php   $order->add_product( get_product('275962'), 1); // This is an existing SIMPLE product   $order->set_address( $address, 'billing' );   //   $order->calculate_totals();   $order->update_status("Completed", 'Imported order', TRUE);   } 

Make sure the product id given should exists in the system.

like image 144
Maha Dev Avatar answered Sep 20 '22 15:09

Maha Dev