How to disable cart page by product id in woocommerce?

Member

by dedrick , in category: PHP CMS , 6 months ago

How to disable cart page by product id in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by addison , 6 months ago

@dedrick 

To disable the cart page for a specific product ID in WooCommerce, you can use the following code snippet in your theme's functions.php file:

1
2
3
4
5
6
7
8
9
add_filter( 'woocommerce_add_to_cart_validation', 'disable_cart_for_specific_product', 10, 3 );
function disable_cart_for_specific_product( $passed, $product_id, $quantity ) {
    // Replace '123' with the product ID you want to disable the cart page for
    if ( $product_id == 123 ) {
        wc_add_notice( __( 'This product cannot be added to the cart.', 'woocommerce' ), 'error' );
        $passed = false;
    }
    return $passed;
}


Replace '123' in the code above with the actual product ID for which you want to disable the cart page. When a customer tries to add that specific product to the cart, they will receive an error message and the product will not be added to the cart.


Remember to always backup your site before making any changes to your theme's functions.php file.