Hide WooCommerce add to cart if product is on backorder but display stock status

43 Views Asked by At

In WooCommerce, does anyone know a way to Remove the Add To Cart button when On Backorder is selected in Inventory, and still keep the On Backorder text on the front end? This would be site wide?

Tried to work this out with:

add_action('woocommerce_single_product_summary', 'check_if_backordered', 1 ); function check_if_backordered(){ 
    global $product; 

    if ($product->is_on_backorder()){ 
       add_filter( 'woocommerce_is_purchasable', '__return_false');
    }
}

But it also removes the Stock Status text.

How to hide add to cart button if product is on backorder but still display the stock status?

1

There are 1 best solutions below

0
LoicTheAztec On

To hide add to cart button if product is on backorder but keep stock status display, try:

add_filter( 'woocommerce_is_purchasable', 'hide_add_to_cart_if_backorder', 20, 2 );
add_filter( 'woocommerce_variation_is_purchasable', 'hide_add_to_cart_if_backorder', 20, 2 );
function hide_add_to_cart_if_backorder( $is_purchasable, $product ) {
    if ( $product->is_on_backorder() ) {
        $is_purchasable = false;
    }
    return $is_purchasable;
}

add_action( 'woocommerce_single_product_summary', 'add_back_backorder_status', 30 );
function add_back_backorder_status() {
    global $product;

    if ( ! $product->is_type('variable') && $product->is_on_backorder() ) {
        echo wc_get_stock_html( $product );
    }
}

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

Tested and works with WooCommerce Storefront theme.