-2

Hope all are well. Been trying to find a solution to the below with no luck.

I have a wordpress/woocommerce store.

I need to be able to only show the add to cart buttons on a certain category...

BUT products sit within different categories and sub categories.

I ideally want to create a category called (BUY) and if the product sits within this category the add to cart features will be enabled. The trouble is products will also sit in other categories so not sure which category overides which. BUY would not be a primary category in the settings.

Hope that makes sense and any help appreciated

  • I have found the below that I have been trying to tweek.. – user3433905 Apr 21 '20 at 11:40
  • add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 ); function remove_add_to_cart_buttons() { if( has_term( 'used-equipment', 'product_cat')) { remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); } } – user3433905 Apr 21 '20 at 11:40

1 Answers1

1

As far understood your question i guess best option would be to use woocommerce_is_purchasable filter... try this

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

function so_61342022_woocommerce_product_purchasable( $purchasable, $product ) {

   return ( has_term( 'buy', 'product_cat' ) ) ? true : false;
}

here 'buy' is your product category slug... change it as your own requirement. so this will disable the buy option from anywhere... ajax, direct url & hide the add to cart button.