Disable add to cart button for an array of products IDs in WooCommerce

后端 未结 1 671
时光取名叫无心
时光取名叫无心 2021-01-14 10:11

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:



        
1条回答
  •  攒了一身酷
    2021-01-14 10:55

    Updated for WooCommerce 3+

    Use in_array() instead like:

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

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

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

    0 讨论(0)
提交回复
热议问题