1

I'm no PHP expert but I'm good enough to scrape some things together here and there. This is my attempt at hiding the WooCommerce add to cart button from specific category pages:

// Remove the add to cart button from certain power line markers
add_action( 'woocommerce_single_product_summary', 'remove_add_cart_button' );
/**
 * Remove add to cart button
 */
function remove_add_cart_button() { 
    
// Categories
$categories = array( 'Power Line Markers' );

 if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
    remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );
 }
}

This works perfectly but it will only remove the add to cart button from specific categories and I'd like to remove it from specific products instead. I'm struggling with the PHP code to do that so I'm hoping someone can put me on the right path.

Thanks in advance and God bless!

1 Answer 1

1

I think you can update the condition of your function remove_add_cart_button to check for products, something like the code below:

/**
 * Remove add to cart button
 */

function remove_add_cart_button(){
    // Products
    $product_ids = array(110, 111, 112); //todo: put your product ids here
    
    if(in_array(get_the_ID(), $product_ids)){
        remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
    }
}
1
  • Thank you, this worked perfectly!
    – Chris J.
    Commented Jan 31, 2023 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.