php - Programmatically creating new order in Woocommerce -
i having hardest time programmatically creating order in woocommerce. using code below , create order cannot customer information or product line items added order. new order create guest no items, user information, etc.
the issue seems once order object created, failing when trying add data order.
function create_vip_order() { global $woocommerce; $address = array( 'first_name' => '111joe', 'last_name' => 'conlin', 'company' => 'speed society', 'email' => 'joe@testing.com', 'phone' => '760-555-1212', 'address_1' => '123 main st.', 'address_2' => '104', 'city' => 'san diego', 'state' => 'ca', 'postcode' => '92121', 'country' => 'us' ); // create order $order = wc_create_order(); // add_product() function below located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php $order->add_product( get_product( '275962' ), 1 ); // 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 error getting in logs:
[19-apr-2016 21:16:38 utc] php fatal error: uncaught error: call 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 on appreciated!
the problem in 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' => 'joe@testing.com', 'phone' => '760-555-1212', 'address_1' => '123 main st.', 'address_2' => '104', 'city' => 'san diego', 'state' => 'ca', 'postcode' => '92121', 'country' => 'us' ); // create order $order = wc_create_order(); // add_product() function below located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php $order->add_product( get_product('275962'), 1); // existing simple product $order->set_address( $address, 'billing' ); // $order->calculate_totals(); $order->update_status("completed", 'imported order', true); }
make sure product id given should exists in system.
Comments
Post a Comment