WooCommerce programmatically add order for a specific user

I am working on a custom WooCommerce plugin. Where in one of my function I want to add an order to the system/database with wc_create_order().

The code beneath works fine, but I can’t find a way to dedicate this order to a specific user (so it shows in the backend and on the accountpage of that user).

Read More

(DB field for this is: ‘_customer_user’ ???)

My code:

// $productid, $userid, $price are passed in the function

$user = get_userdata($userid);

$address = array(
        'first_name' => get_user_meta( $userid, "billing_first_name", true),
        'last_name'  => get_user_meta( $userid, "billing_last_name", true),
        '......'
);

$order = wc_create_order();

$args['totals']['subtotal'] = $price;
$args['totals']['total'] = $price;

$order->add_product( get_product( $productid ), 1, $args );
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->calculate_totals();

Related posts

1 comment

  1. You can do that with update_post_meta

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

Comments are closed.