TheProphet
TheProphet

Reputation: 93

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

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

Upvotes: 4

Views: 9062

Answers (4)

Ashwani Shukla
Ashwani Shukla

Reputation: 628

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

and the customer_id will be automatically set by woocommerce.

Upvotes: 0

mithun raval
mithun raval

Reputation: 180

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);

Upvotes: 6

LoicTheAztec
LoicTheAztec

Reputation: 254378

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 );

Upvotes: 3

Ali Shan
Ali Shan

Reputation: 26

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

Upvotes: 0

Related Questions