我正在写一个WooCommerce插件,负责支付和交付。 现在我正在根据当前购物车创建订单。 一切正常,项目和成本正确,唯一的问题是订单显示为由“访客”而不是当前登录的用户(即使该用户的正确电子邮件地址在订单上) 。
这是我的代码:
$cart = WC()->cart;
$checkout = WC()->checkout();
$order_id = $checkout->create_order();
$order = wc_get_order( $order_id );
$order->user_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );
$order->calculate_totals();
$order->payment_complete();
$cart->empty_cart();
这是我在运行之后在后端看到的内容:
为什么订单被设置为“访客”而不是下订单的用户? 如何连接正确的用户?
您需要将_customer_user
添加到带有订单ID的post_meta表,元值应该是您要链接到此订单的用户ID。
我希望这会对你有所帮助:
update_post_meta($order_id, '_customer_user', get_current_user_id());
要么
在wc_create_order();
中使用用户/客户ID(在下订单之前创建客户/用户)
例如:$order = wc_create_order(array('customer_id' => $user));
这段代码对我来说很好!