在WooCommerce中禁用一系列产品ID的“添加到购物车”按钮 [英] Disable add to cart button for an array of products IDs in WooCommerce

查看:65
本文介绍了在WooCommerce中禁用一系列产品ID的“添加到购物车”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我试图禁用一系列产品ID的添加到购物车按钮,但找不到问题。

In WooCommerce, I'm trying to disable add to cart button for an array of product IDs but I can't find the problem.

我正在尝试使用此功能:

I am trying to use this function:

add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);

function my_woocommerce_is_purchasable($is_purchasable, $product) {
    $id=check(); // This function return an array of IDs
    foreach ($id as $id_p){
        return ($product->id = $id_p ? false : $is_purchasable);
    }
}

这是我的支票()功能代码(update)

function check() { 
    $listproduit = get_woocommerce_product_list();
    $score = get_score_user(); 
    foreach ($listproduit as $products) { 
        if ($products[1] >= 5000) { 
            $listid = $products[0]; 
            return $listid; 
            // print_r($listid); 
        } 
    } 
    return $listid; 
}

但这不起作用。

我在做什么错了?

谢谢

推荐答案

已更新为WooCommerce 3 +

使用 in_array()代替:

add_filter('woocommerce_is_purchasable', 'filter_is_purchasable', 10, 2);
function filter_is_purchasable($is_purchasable, $product ) {
    if( in_array( $product->get_id(), not_purchasable_ids() ) {
         return false;
    } 
    return is_purchasable;
}

其中 not_purchasable_ids()是返回非购买产品ID的数组的函数(在此简化)

Where not_purchasable_ids() is the function that returns an array of non purchasable products Ids (here simplified):

function not_purchasable_ids() {
     return array( 37, 53, 128, 129 );
}

这段代码包含在您的活动子主题(或活动主题)的functions.php文件中。经过测试并可以使用。

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

这篇关于在WooCommerce中禁用一系列产品ID的“添加到购物车”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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