普通网友 2016-09-26 11:19
浏览 49
已采纳

在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);
    }
}

And this is my check() function code (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; 
}

But this doesn't work.

What am I doing wrong?

Thanks

  • 写回答

1条回答 默认 最新

  • dq1230123 2016-09-26 11:47
    关注

    You have to return true or false and to include $is_purchasable in your condition.

    Try this code:

    add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
    function my_woocommerce_is_purchasable($is_purchasable, $product) {
        $id = check();
        if(in_array($product->id, $id) && $is_purchasable) return false;
        else return true;
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The code is tested and fully functional.


    Update with the code of your check() function. I can't test this, but I have made some changes in it.

    Here is the code

    function check() { 
        $listproduit = get_woocommerce_product_list();
        $listid = array();
        $score = get_score_user(); 
        foreach ($listproduit as $products) { 
            if ($products[1] >= 5000) { 
                $listid[] = $products[0];   
            } 
        } 
        // print_r($listid);
        return $listid; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来