检查客户是否已经在 WooCommerce 中购买了东西 [英] Checking if customer has already bought something in WooCommerce

查看:44
本文介绍了检查客户是否已经在 WooCommerce 中购买了东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 WooCommerce 插件来为客户(有购买历史记录)添加一些优惠.

如何查看用户之前购买过的东西?

谢谢.

解决方案

2020 年更新:新的更新改进、更轻、更快的版本此处,还可以处理来自账单电子邮件的客人


<块引用>

是的,可以创建一个条件函数,当客户已经至少有一个订单时返回true> 状态已完成.

这是这个条件函数的代码:

function has_bought() {//获取所有客户订单$customer_orders = get_posts( 数组('数字帖子' =>1,//一个订单就够了'meta_key' =>'_customer_user','元值' =>get_current_user_id(),'post_type' =>'shop_order',//WC 订单帖子类型'post_status' =>'wc-completed',//只有已完成"的订单;地位'字段' =>'ids',//返回 Ids 已完成") );//返回真"当客户已经至少有一个订单时(如果没有则为假)返回计数($customer_orders)>0 ?真假;}

此代码已经过测试且有效.

此代码位于活动子主题或主题的 function.php 文件中,或位于插件 php 文件中.


用法(作为条件):

  • 您可以在您将拥有的一些 WooCommerce 模板中使用它之前复制到您的活动子主题或主题.
  • 在您的主题 php 文件中.
  • 在插件 php 文件中.
  • 任何 php 函数或 WordPress/WooCommerce.

参考资料

I would like to create a WooCommerce plugin to add some offers for customers (that have a purchase History).

How can I check a user bought something before?

Thanks.

解决方案

Update 2020: New updated improved, light and faster version HERE that handle also guests from billing email


Yes it is possible creating a conditional function that return true when a customer has already at least one order with status completed.

Here is the code for this conditional function:

function has_bought() {
    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => 1, // one order is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed', // Only orders with "completed" status
        'fields'      => 'ids', // Return Ids "completed"
    ) );

    // return "true" when customer has already at least one order (false if not)
   return count($customer_orders) > 0 ? true : false; 
}

This code is tested and works.

This code goes in function.php file of your active child theme or theme, or in a plugin php file.


USAGE (as a condition):

  • You can use it in some WooCommerce templates that you will have previously copied to your active child theme or theme.
  • In your theme php files.
  • In plugin php files.
  • Any php function or WordPress/WooCommerce.

References

这篇关于检查客户是否已经在 WooCommerce 中购买了东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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