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
Advertisement
Answer
Updated for WooCommerce 3+
Use in_array()
instead like:
add_filter( 'woocommerce_variation_is_purchasable', 'filter_is_purchasable', 10, 2 ); 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.