How Can I Create An Woocommerce Order Programmatically

How can I create an Woocommerce Order Programmatically

You can use wc_create_order() to setup an Woocommerce order object and assign data you would like.

global $woocommerce;
$address = array(
      'first_name' => 'Test',
      'last_name'  => 'Test',
      'company'    => 'Test',
      'email'      => 'test@softwarehtec.com',
      'phone'      => '00-00-0000',
      'address_1'  => '0 test st',
      'address_2'  => '',
      'city'       => 'Test',
      'state'      => 'NY',
      'postcode'   => '10010',
      'country'    => 'US'
);


$order = wc_create_order();
$order->add_product( get_product('1'), 1); 
$order->set_address( $address, 'billing' );

$order->calculate_totals();
$order->update_status("completed", 'Created order programmatically', TRUE);  
0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sajjad Hossain Sagor
4 years ago

Great Post! But how do i assign this order to an customer? And Update Status ‘completed’… isn’t it should be ‘wc-completed’?? And if you can please publish a post about how to trigger woocommerce payment process like checkout but it should process in the background!!