Programmatically creating new order in Woocommerce

Solution for : Programmatically creating new order in Woocommerce

We can create new order in Woocommerce using be hook.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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'
  );
 
  // 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.

No comments:

Post a Comment