skip to Main Content

At the moment, if an item is out of stock a message appears in the checkout page:

Sorry, we do not have enough "xxx" in stock to fulfil your order (2 available). We apologise for any inconvenience caused.

Hence If that message appears, then I want to disable the "Proceed to checkout" button on the page by simply removing it.

If it that message doesn’t appear, then display the "Proceed to checkout" button.

I’ll obviously create an IF statement so that if it’s true then display if not, don’t display etc.

Here’s my "Proceed to checkout" button code:

<input type="submit" name="cart_submit" class="checkout-button button alt wc-forward" style="text-transform: capitalize;" value="<?php esc_html_e( 'Proceed to checkout', 'woocommerce' ); ?>" />

So is there a method that I can call in Woocommerce to see if that error message has been called?

I tried scanning the entire webpage for that message and if true, execute xyz but that didn’t work (could be sessions).

Any help would greatly be appreciated.

2

Answers


  1. Woocommerce has a special feature of having product value using global $product.
    you can access your product stock quantity using the following code.

    global $product;
    $quantity = $product->get_stock_quantity();
    if($quantity < 1){
      //take decision. 
    }
    
    Login or Signup to reply.
  2. Based on WC_Cart check_cart_item_stock() method source code, that displays the error code of your question, The first function will check cart items stock as a conditional function returning true if everything is all right or false if an error has been displayed mentioning that there is a stock issue on cart items.

    The second function will display a disabled greyed "Place Order" button if there is a stock issue on cart items.

    function wc_check_cart_item_stock() {
        $product_qty_in_cart      = WC()->cart->get_cart_item_quantities();
        $current_session_order_id = isset( WC()->session->order_awaiting_payment ) ? absint( WC()->session->order_awaiting_payment ) : 0;
    
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $product = $values['data'];
    
            // Check stock based on stock-status.
            if ( ! $product->is_in_stock() ) {
                return false;
            }
    
            // We only need to check products managing stock, with a limited stock qty.
            if ( ! $product->managing_stock() || $product->backorders_allowed() ) {
                continue;
            }
    
            // Check stock based on all items in the cart and consider any held stock within pending orders.
            $held_stock     = wc_get_held_stock_quantity( $product, $current_session_order_id );
            $required_stock = $product_qty_in_cart[ $product->get_stock_managed_by_id() ];
    
            if ( $product->get_stock_quantity() < ( $held_stock + $required_stock ) ) {
                return false;
            }
        }
        return true;
    }
    
    add_filter( 'woocommerce_order_button_html', 'disable_order_button_html' );
    function disable_order_button_html( $button ) {
        if( wc_check_cart_item_stock() ) {
            return $button;
        } else {
            return '<a class="button alt disabled" style="cursor:not-allowed; text-align:center">' .__('Place order', 'woocommerce') . '</a>';
        }
    }
    

    Disabled greyed "Place order" button on checkout page

    enter image description here

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


    If you want to remove the checkout "Place Order" button instead replace:

    return '<a class="button alt disabled" style="cursor:not-allowed; text-align:center">' .__('Place order', 'woocommerce') . '</a>';
    

    with:

    return '';
    

    Related: Customizing checkout "Place Order" button output html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search