如果客户已购买特定产品,则禁用WooCommerce产品一段时间 [英] Disable WooCommerce products for some time if customer has purchased specific product

查看:62
本文介绍了如果客户已购买特定产品,则禁用WooCommerce产品一段时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,如果客户购买了产品(A或B),则在一个月内要禁用对定义产品(B,C和D)的购买,因此产品B,C和D应该只能对该特定用户禁用一个月.

In WooCommerce would like to disable purchases from defined products ( B, C and D ) during a month if customer has purchased a product ( A or B ), so products B, C and D should be disabled for a month for that specific user only.

注意:在下面的代码中,我正在使用 has_bought_items()来自

Note: In the code below I am using has_bought_items() a custom function from Check if a user has purchased specific products in WooCommerce answer.

我的代码尝试:

$product_ids = array( 255, 256 );

$args2 = array(
    'customer_id' => get_current_user_id()
);
$orders = wc_get_orders( $args2 );
$orders = has_bought_items( get_current_user_id(), $product_ids );

if($orders){
    add_filter('woocommerce_is_purchasable', 'disable_product', 10, 2 );
 
    function disable_product( $is_purchasable, $product ) {
 
        if( in_array( $product->get_id(), array( 256, 257, 258 ) ) ) {
            return false;
        }
 
        return $is_purchasable;
    }
}

So far I am able to disable the products for the user. But I don't how can I get date from the purchase and add a month to it. Any help will be great. 

推荐答案

您需要自定义 has_bought_items()中的代码,以获取最高的发布日期".从购买了产品ID为 255 256 (针对当前用户ID)的客户订单中获得.

You need to customize a bit the code from has_bought_items() to get the highest "post date" from a customer order that has purchased product id 255 or 256 (for the current user ID).

然后使用 strtotime()函数,您将可以在一个月内禁用其他已定义产品的购买.

Then using strtotime() function you will be able to disable purchases on other defined products during a month.

使用自定义SQL查询的代码:

The code using a customized SQL query:

add_filter('woocommerce_is_purchasable', 'disable_specific_product_conditionally', 10, 2 );
function disable_specific_product_conditionally( $is_purchasable, $product ) {
    $targeted_ids = array( 256, 257, 258 ); // Related targeted products to be disabled

    if( in_array( $product->get_id(), $targeted_ids ) && is_user_logged_in() ) {
        global $wpdb;

        $product_ids   = array( 255, 256 ); // Check orders for this products

        // Count the number of products
        $date = $wpdb->get_var("
            SELECT p.post_date FROM {$wpdb->prefix}posts AS p
            INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
            INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
            INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
            WHERE p.post_status IN ( 'wc-" . implode( "','wc-", array_map( 'esc_sql', wc_get_is_paid_statuses() ) ) . "' )
            AND pm.meta_key = '_customer_user'
            AND pm.meta_value = '".get_current_user_id()."'
            AND woim.meta_key IN ( '_product_id', '_variation_id' )
            AND woim.meta_value IN (".implode(',', $product_ids).")
            ORDER BY p.post_date DESC
        ");
        
        // When a date is found we disable during a month purchases from this date
        return ! empty($date) && strtotime('now') > strtotime( $date . ' + 1 month') ? false : $is_purchasable;
    }
    return $is_purchasable;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于如果客户已购买特定产品,则禁用WooCommerce产品一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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