WooCommerce codex:如何在订单上设置用户/客户

问题描述 投票:3回答:2

我正在写一个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();

这是我在运行之后在后端看到的内容:

enter image description here


为什么订单被设置为“访客”而不是下订单的用户? 如何连接正确的用户?

php wordpress woocommerce
2个回答
5
投票

您需要将_customer_user添加到带有订单ID的post_meta表,元值应该是您要链接到此订单的用户ID。

我希望这会对你有所帮助:

update_post_meta($order_id, '_customer_user', get_current_user_id());

1
投票

要么

wc_create_order();中使用用户/客户ID(在下订单之前创建客户/用户)

例如:$order = wc_create_order(array('customer_id' => $user));

这段代码对我来说很好!

© www.soinside.com 2019 - 2024. All rights reserved.