Disable add to cart button based on WooCommerce product custom stock status Disable add to cart button based on WooCommerce product custom stock status wordpress wordpress

Disable add to cart button based on WooCommerce product custom stock status


You can use the following to disable add to cart button based on a custom stock status (where you will replace custom_status_slug by your custom status slug):

add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );function filter_is_purchasable_callback( $purchasable, $product ) {    if ( $product->get_stock_status() === 'custom_status_slug' ) {        return false;    }    return $purchasable;}

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


I propose a solution that displays the stock status on the product page but not the 'add to cart' button

add_filter('woocommerce_product_is_in_stock', 'filter_is_in_stock_callback', 10, 2 );function filter_is_in_stock_callback( $stock, $product ) {    if ( $product->get_stock_status() === 'custom_status_slug' ) {        return false;           }    return $stock;}

Thanks for the first answer :)