检查用户是否在WooCommerce中购买了特定产品

阿莱格霍斯

我需要检查客户是否在WooCommerce之前购买了特定产品。

情况是这样的:除非他们较早购买了产品“ a”或“ b”,否则客户将不能购买产品“ c”,“ d”,“ e”。

如果客户较早购买了产品“ a”或“ b”,则激活产品“ c”,“ d”和“ e”的购买按钮,并允许他们购买产品。

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

我怎样才能做到这一点?

谢谢。

LoicTheAztec

打火机和改进的代码版本在这里是手柄多个产品ID

更新 (与Woocommerce 3+兼容)

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

这是条件函数:

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模板使用它,这些模板先前已复制到您的活动子主题或主题:

这是您可以在上面的模板中使用的示例

// 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 );

}

您将greyed_buttonstyle.css活动子主题或主题文件中使用class设置非活动按钮的样式greyed_button-message消息相同

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

限制用户在woocommerce中再次购买特定产品

来自分类Dev

如果客户购买了特定产品,则禁用WooCommerce产品一段时间

来自分类Dev

检查客户是否购买了商品并将产品添加到WooCommerce中的购物车

来自分类Dev

Woocommerce 中登录用户的单一特定产品购买

来自分类Dev

检查客户是否已经在WooCommerce中购买了商品

来自分类Dev

检查产品是否属于 Woocommerce 中的特定产品类别

来自分类Dev

检查用户是否购买了商品并重定向到特定页面

来自分类Dev

WooCommerce根据购买的特定产品的数量收取额外费用

来自分类Dev

仅当在 Woocommerce 中应用优惠券时才允许购买特定产品

来自分类Dev

购买特定产品后添加用户元

来自分类Dev

检查是否购买了应用

来自分类Dev

根据WooCommerce中的先前订单和电话号码,限制来宾用户在同一周内多次购买特定产品

来自分类Dev

如果在 Woocommerce 中购买了特定的变体,则将可变产品设置为起草

来自分类Dev

在 Woocommerce 产品页面中隐藏特定产品标签的按钮

来自分类Dev

隐藏WooCommerce中未登录用户的特定产品变体

来自分类Dev

如果已登录的用户已经购买了产品,请在结帐页面上检查

来自分类Dev

仅显示购买特定产品而不购买其他特定产品的记录

来自分类Dev

在WooCommerce结帐中更改特定产品的付款方式标题

来自分类Dev

查找特定产品是否存在于Firebase中

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

要求在WooCommerce中注册特定产品

来自分类Dev

Woocommerce 隐藏特定产品的价格

来自分类Dev

Xtext禁用特定产品的验证检查

来自分类Dev

从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

来自分类Dev

我如何知道哪个Google帐户(电子邮件ID)用户在我的应用中购买了产品?

来自分类Dev

在Woocommerce单个产品页面中显示特定产品标签的自定义内容

来自分类Dev

从 Woocommerce 的特定产品类别中删除“未找到产品”

Related 相关文章

  1. 1

    限制用户在woocommerce中再次购买特定产品

  2. 2

    如果客户购买了特定产品,则禁用WooCommerce产品一段时间

  3. 3

    检查客户是否购买了商品并将产品添加到WooCommerce中的购物车

  4. 4

    Woocommerce 中登录用户的单一特定产品购买

  5. 5

    检查客户是否已经在WooCommerce中购买了商品

  6. 6

    检查产品是否属于 Woocommerce 中的特定产品类别

  7. 7

    检查用户是否购买了商品并重定向到特定页面

  8. 8

    WooCommerce根据购买的特定产品的数量收取额外费用

  9. 9

    仅当在 Woocommerce 中应用优惠券时才允许购买特定产品

  10. 10

    购买特定产品后添加用户元

  11. 11

    检查是否购买了应用

  12. 12

    根据WooCommerce中的先前订单和电话号码,限制来宾用户在同一周内多次购买特定产品

  13. 13

    如果在 Woocommerce 中购买了特定的变体,则将可变产品设置为起草

  14. 14

    在 Woocommerce 产品页面中隐藏特定产品标签的按钮

  15. 15

    隐藏WooCommerce中未登录用户的特定产品变体

  16. 16

    如果已登录的用户已经购买了产品,请在结帐页面上检查

  17. 17

    仅显示购买特定产品而不购买其他特定产品的记录

  18. 18

    在WooCommerce结帐中更改特定产品的付款方式标题

  19. 19

    查找特定产品是否存在于Firebase中

  20. 20

    要求在WooCommerce中注册特定产品

  21. 21

    要求在WooCommerce中注册特定产品

  22. 22

    要求在WooCommerce中注册特定产品

  23. 23

    要求在WooCommerce中注册特定产品

  24. 24

    Woocommerce 隐藏特定产品的价格

  25. 25

    Xtext禁用特定产品的验证检查

  26. 26

    从所有产品中获取 Woocommerce 中特定产品属性的术语名称数组

  27. 27

    我如何知道哪个Google帐户(电子邮件ID)用户在我的应用中购买了产品?

  28. 28

    在Woocommerce单个产品页面中显示特定产品标签的自定义内容

  29. 29

    从 Woocommerce 的特定产品类别中删除“未找到产品”

热门标签

归档