WooCommerce以编程方式为特定用户添加订单

时间:2015-06-26 10:56:50

标签: woocommerce wordpress

我正在开发一个自定义的WooCommerce插件。在我的一个函数中,我想用wc_create_order()向系统/数据库添加订单。

下面的代码工作正常,但我找不到将此订单专用于特定用户的方法(因此它显示在该用户的后端和帐户页面上)。

(DB字段为:'_ customer_user'???)

我的代码:

// $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();

1 个答案:

答案 0 :(得分:3)

您可以使用update_post_meta

执行此操作
update_post_meta( $order->id, '_customer_user', $userid );