WooCommerce 以编程方式为特定用户添加订单
WooCommerce programmatically add order for a specific user
我正在开发自定义 WooCommerce 插件。在我的一个函数中,我想用 wc_create_order()
.
向 system/database 添加订单
下面的代码工作正常,但我找不到将此订单专用于特定用户的方法(因此它显示在后端和该用户的帐户页面上)。
(这个的数据库字段是:'_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();
你可以用 update_post_meta
update_post_meta( $order->id, '_customer_user', $userid );
我正在开发自定义 WooCommerce 插件。在我的一个函数中,我想用 wc_create_order()
.
下面的代码工作正常,但我找不到将此订单专用于特定用户的方法(因此它显示在后端和该用户的帐户页面上)。
(这个的数据库字段是:'_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();
你可以用 update_post_meta
update_post_meta( $order->id, '_customer_user', $userid );