Assign the user ID to an order created programmatically in Woocommerce 3

woocommerce create order programmatically
create custom order woocommerce programmatically
woocommerce create subscription programmatically
unable to create order error in woocommerce
get order id woocommerce
wp create order
woocommerce order object
woocommerce order->has_status

I’m using Woocommerce to manage my products on Wordpress, but the user pay through a 3rd party service.

I’m trying to create new order manually, and assign it to the current user.The order is indeed created and logged but the following code only log the item and the time, no details of the user:

  global $woocommerce;

  $address = array(
      'first_name' => $_GET['FirstName'],
      'last_name'  => $_GET['LastName'],
      'company'    => $_GET['CompanyName'],
      'email'      => $_GET['EmailAddress'],
      'phone'      => $_GET['MobilePhoneNumber'],
      'address_1'  => '',
      'address_2'  => '',
      'city'       => '',
      'state'      => '',
      'postcode'   => '',
      'country'    => $_GET['countryCode']
  );

  $order = wc_create_order();

  foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
            $item_id = $order->add_product(
                    $values['data'], $values['quantity'], array(
                'variation' => $values['variation'],
                'totals' => array(
                    'subtotal' => $values['line_subtotal'],
                    'subtotal_tax' => $values['line_subtotal_tax'],
                    'total' => $values['line_total'],
                    'tax' => $values['line_tax'],
                    'tax_data' => $values['line_tax_data'] // Since 2.2
                )
                    )
            );
        }
  $order->set_address( $address, 'billing' );
  //
  $order->calculate_totals();
  $order->update_status("Completed", 'Imported order', TRUE);  
$order->save();

The admin can see what was ordered (under woocommerce->orders) but not by whom, and the user can’t see his orders at all.

I've uploaded the website to a temporary domain so you guys can see: https://360transformations.000webhostapp.com/?page_id=446

Assign the user ID to an order, Use below options

Option 1:

$userid = get_current_user_id();
$order = wc_create_order(array('customer_id'=>$userid));

Option 2:

add below code after wc_create_order() function

$userid = get_current_user_id();
update_post_meta($order->id, '_customer_user', $userid);

php, Assign the user ID to an order, Use below options. Option 1: $userid = get_current_user_id(); $order  When an order is placed in WooCommerce, you might want to change/add something in the User Meta programmatically. For example, you could “check” a custom checkbox in the User Profile. Or maybe assign the User Twitter username.

You will have to add the customer ID using set_customer_id() CRUD setter method:

$order->set_customer_id( $user_id );

Where $user_id should be the current user Id or a defined user ID… This Id will be 0 for guest users. To get the current user ID you will use:

$user_id = get_current_user_id();
$order->set_customer_id( $user_id );

how to use wc_create_order with subscription product, I'm using Woocommerce to manage my products on WordPress, but the user pay through a 3rd party service. I'm trying to create new order  WooCommerce Creating Order Programmatically. GitHub Gist: instantly share code, notes, and snippets.

Woocommerce link an order with the user by post's metadata. When a new order is placed then woocommerce creates a new post. And sets _customer_user meta key to the user id that is placing the order. So, in case you want to link an order with some other user; say instance with user B that has user id 20. Then just update the meta value of _customer_user to 20 of a particular order (post).

Moreover, there are other meta keys from where wooocommerce to get the first name, last name, and other information. You may also require to update those meta values according to new user information.

So, just update the meta values and you are good to go.

Check the below image

woocommerce - Post meta key that is responsible to link an order to the user.

Possible Duplicate:

Programmatically creating new order in Woocommerce

WooCommerce Creating Order Programmatically · GitHub, far as I can see, you need to create the order first, then the sub $order = wc_create_order(array('customer_id' => $user->id)); $order->add_product( $​product,  Adding an item to cart programmatically is the same as “automatically”. Basically, all users will have a default, non-empty Cart filled with an item of your choice. So, let’s see how this snippet works!

You can do this https://developer.wordpress.org/reference/functions/wp_set_current_user/

and the customer_id will be automatically set by woocommerce.

Create WooCommerce order for new customers, creating Woocommerce order with line_item programatically //(get_product with id and next is for quantity) $order->set_address( $address,  Download the extension from your WooCommerce dashboard. Go to Plugins > Add New > Upload and select the ZIP file you just downloaded. Click Install Now, and then Activate. Go to WooCommerce > Settings > Order Statuses and read the next section to learn how to setup and configure the plugin.

Class WC_Order, Automatically create an order with a free download when a customer account is created. Following on from last week's Programmatically create a WooCommerce order for a The user of this plugin must write a single line of code to set the ID of the downloadable product. November 25, 2018 at 3:50 pm. Most of the time this should mark an order as 'processing' so that admin can process/post the items. If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action. Stock levels are reduced at this point. Sales are also recorded for products. Finally, record the date of payment. Parameters

Programmatically Create A WooCommerce Order, Gets the order number for display (by default, order ID). Gets the order Get created via. Maybe set empty billing email to that of the user who owns the order. Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

How to create WordPress users programmatically, Programmatically Create A WooCommerce Order 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 We already have assigned $user_id the user ID of the customer placing the order. To create the category, I used an array with the data per category needed for the function wp_insert_term. Then i looped trough that array,and used a fetch_media function that uploads the image found in the image-path given to that function, and returns an attachment ID.