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

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

问题描述

我想创建一个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?

谢谢.

推荐答案

更新:新更新得到了改进,轻巧和更快的版本 HERE


是的是可能的.创建一个条件功能,当客户已经至少有一个订单状态为true >已完成.

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,
        '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 status "completed"
    ) );

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

此代码已经过测试并且可以正常工作.

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

用法(作为条件):

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.

参考

  • Template Structure + Overriding Templates via a Theme
  • Check if a customer has purchased a specific products in WooCommerce

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

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