检查用户是否在WooCommerce中购买了特定产品 [英] Check if a user has purchased specific products in WooCommerce

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

问题描述

我需要检查客户是否早些时候在WooCommerce中购买了特定产品.

I need to check if a customer has purchased a specific product earlier in WooCommerce.

情况是这样的:除非客户较早购买了产品"a"或"b",否则客户将无法购买产品"c","d","e".

The case is this: The customer shall not be able to purchase product "c", "d", "e" unless they have purchased product "a" or "b" at an earlier time.

如果客户较早购买了产品"a"或"b",那么将激活产品"c","d"和"e"的购买按钮,并允许他们购买产品.

If the customer has purchased product "a" or "b" earlier, then the purchase button of product "c", "d" and "e" is activated and they are allowed to buy them.

如果他们之前没有购买过"a"或"b",则将不允许他们购买"c","d","e",并且停用了购买按钮.

If they haven´t purchased "a" or "b" earlier, they will not be allowed to purchase "c", "d", "e" and the purchase button is deactivated.

我该如何实现?

谢谢.

推荐答案

此处轻巧和改进的代码版本 ,用于处理多个产品ID

Lighter and improved code version in HERE that handle multiple product IDs

更新 (与Woocommerce 3+兼容)

是的,如果当前客户已经购买了特定定义的产品ID,则可以编写条件函数返回"true". 此代码位于您的活动子主题或主题的function.php文件上.

Yes it's possible, writing a conditional function that returns "true" if current customer has already bought specifics defined products IDs. This code goes on function.php file of your active child theme or theme.

这是条件函数:

function has_bought_items() {
    $bought = false;

    // Set HERE ine the array your specific target product IDs
    $prod_arr = array( '21', '67' );

    // 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"
    ) );
    foreach ( $customer_orders as $customer_order ) {
        // Updated compatibility with WooCommerce 3+
        $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
        $order = wc_get_order( $customer_order );

        // Iterating through each current customer products bought in the order
        foreach ($order->get_items() as $item) {
            // WC 3+ compatibility
            if ( version_compare( WC_VERSION, '3.0', '<' ) ) 
                $product_id = $item['product_id'];
            else
                $product_id = $item->get_product_id();

            // Your condition related to your 2 specific products Ids
            if ( in_array( $product_id, $prod_arr ) ) 
                $bought = true;
        }
    }
    // return "true" if one the specifics products have been bought before by customer
    return $bought;
}

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

用法:
例如,您可以在某些 WooCommerce模板 您之前已将其复制到活动的子主题或主题:

USAGE:
For example, you can use it in some WooCommerce templates that you will have previously copied to your active child theme or theme:

  • add-to-cart 按钮相关的商店页面模板为 loop/add-to-cart.php .
  • add-to-cart 按钮有关的产品页面模板位于
  • The template for Shop page concerning add-to-cart button is loop/add-to-cart.php.
  • The templates for Product pages concerning add-to-cart button are in single-product/add-to-cart folder depending on your product types.

以下是您可以在以下模板中使用的示例:(上方):

Here is an example that you could use in those templates (above):

// Replace the numbers by your special restricted products IDs
$restricted_products = array( '20', '32', '75' );

// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

// customer has NOT already bought a specific product for this restricted products
if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) { 

    // Displaying an INACTIVE add-to-cart button (With a custom text, style and without the link).
    // (AND optionally) an explicit message for example.

// ALL OTHER PRODUCTS OR RESTRICTED PRODUCTS IF COSTUMER HAS ALREADY BOUGHT SPECIAL PRODUCTS
} else { 

    // place for normal Add-To-Cart button code here
}

商店页面上的 add-to-cart 按钮模板上,完整的应用示例:

<?php
/**
 * Loop Add to Cart
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/add-to-cart.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.5.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

// Replace the numbers by your special restricted products IDs
$restricted_products = array( '37', '53', '70' );

// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

if ( !has_bought_items() && in_array( $product_id, $restricted_products ) ) {

    echo '<a class="button greyed_button">' . __("Disabled", "your_theme_slug") . '</a>';
    echo '<br><span class="greyed_button-message">' . __("Your message goes here…", "your_theme_slug") . '</span>';

} else {

echo apply_filters( 'woocommerce_loop_add_to_cart_link',
    sprintf( '<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product_id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $class ) ? $class : 'button' ),
        esc_html( $product->add_to_cart_text() )
    ),
$product );

}

您将为处于活动状态的子主题或主题的 style.css 文件中的 greyed_button 类设置非活动按钮的样式.与 greyed_button-message 类的消息相同.

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

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