WooCommerce 法典:如何在订单上设置用户/客户 [英] WooCommerce codex : how to set the user/customer on an order

查看:24
本文介绍了WooCommerce 法典:如何在订单上设置用户/客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 WooCommerce 插件来处理付款和交付.
现在我正在根据当前的购物车创建订单.
一切正常,使物品和成本正确无误,唯一的问题是订单显示为由客人"制作,而不是由当前登录的用户制作(即使正确的电子邮件地址为该用户在订单中).

I am writing a WooCommerce Plugin that takes care of payment and delivery.
Right now I am at the point of creating an order based on the current shopping cart.
That all works fine getting the items and costs correct, the only problem is that the order shows as being made by "Guest", instead of by the currently logged on user (even though the correct email address for that user is on the order).

这是我的代码:

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

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

Here is what I see in the backend after running this :

为什么以访客"身份而不是下订单的用户下订单?
我如何才能附加正确的用户?

推荐答案

您需要将 _customer_user 添加到 post_meta 表,订单 ID 和元值应该是您要链接到的用户 ID这个顺序.

You will need to add _customer_user to post_meta table with order ID and meta value should be the user ID which you want to link to this order.

希望对您有所帮助:

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

这篇关于WooCommerce 法典:如何在订单上设置用户/客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆