Disable Add to Cart Button for an Array of Products Ids in Woocommerce

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

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.

Removing Add to Cart button for mutiple Products

Simply replace comparsion with in_array method like this:

$nonPurchaseableProductsIds = [118773, 118774, 118775, 118776];

add_filter( 'filter_non_purchaseable_items', function ($product) use (nonPurchaseableProductsIds ) {
return !in_array($product->get_id(), nonPurchaseableProductsIds);
});

This will return FALSE if item should be disabled. Of course we assume your $product object has get_id() method

Remove add cart button in Woocommerce for a specific product category

Nov 2020 Update

To make it work with a product category you can use the WordPress conditional function has_term() this way:

add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
function remove_product_description_add_cart_button() { // function for deleting ...
// Set HERE your category ID, slug or name (or an array)
$categories = array('your-category-1');

//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}

Code goes in function.php file of your active child theme (or active theme). Or also in any plugin php file. Tested and works.

Avoid add to cart for specific product categories if user is unlogged in Woocommerce

The conditional tag is_product_category() only target product category archive pages. Instead you can use WordPress conditional function has_term().

There is 2 ways to avoid specific products being added to cart for no logged user…

1) Using Add to cart validation hook:

// Avoid add to cart conditionally
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_conditionally', 20, 3 );
function avoid_add_to_cart_conditionally( $passed, $product_id, $quantity) {
// HERE your product categories (can be IDs, slugs or names terms)
$terms = array( 'gekoelde-bier', 'bierkoerier');

if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
// Displaying a custom notice (optional)
wc_add_notice( __('Only logged in users are allowed to purchase this item. Please register.'), 'error' );

$passed = false;
}

return $passed;
}

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

Sample Image


2) Using is_purchasable product property (It will remove add to cart button):

add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
function conditional_purchasable_products( $is_purchasable, $product ) {
// HERE your product categories (can be IDs, slugs or names terms)
$terms = array( 'gekoelde-bier', 'bierkoerier');

$product_id = $product->get_id(); // The product ID

if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
$is_purchasable = false;
}

return $is_purchasable;
}

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



Targeting the parent product category terms too.

You will use the following custom conditional function to replace has_term() Wordpress function:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;

if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

Then for both hooked functions, you will replace the following line :

if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){

By this line:

if( has_product_categories( $terms, $product_id ) && ! is_user_logged_in() ){

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

Disable add-to-cart for certain categories only matches the first category in array (Woocommerce)

Using the second example here: https://stackoverflow.com/a/53058011/5204226 works (I had to change has_terms to has_term).

function conditional_purchasable_products( $is_purchasable, $product ) {
// HERE your product categories (can be IDs, slugs or names terms)
$terms = array( 'sneakers', 'backpacks');

$product_id = $product->get_id(); // The product ID

if( ! has_term( $terms, 'product_cat', $product_id ) ){
$is_purchasable = false;
}

return $is_purchasable;
}
add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);

Woocommerce hide add to cart button EXCEPT for variable products

add_action('woocommerce_single_product_summary', 'wp66176371_remove_product_description_add_cart_button', 1 );
function wp66176371_remove_product_description_add_cart_button() {
global $product;
if ( !empty($product) && $product->is_type( 'simple' ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}


Related Topics



Leave a reply



Submit