Reece
Reece

Reputation: 2701

WooCommerce how to overwrite the product quantity

How can I override the current quantity input with the following code? I don't know which hook to use hook in my function. I want the quantity on the product page and basket to change

function quantity(){
    global $product;

    echo 
    '<div class="quantity">
        <p>Quantity: </p>
        <button class="increment">
            <span></span><span></span>
        </button>
        <input type="text" value="1" min="1" max="' . $product->get_stock_quantity() . '">
        <button class="decrement">
            <span></span>
        </button>
    </div>';
}

I have found this function that allows me to change to quantity input but it causes the following error on the basket page.

Fatal error: Call to a member function get_stock_quantity() on null

Here's the code;

function woocommerce_quantity_input() {
    global $product;
    echo 
        '<div class="quantity">
            <p>Quantity: </p>
            <button class="increment">
                <span></span><span></span>
            </button>
            <input type="text" value="1" min="1" max="' . $product->get_stock_quantity() . '">
            <button class="decrement">
                <span></span>
            </button>
        </div>';

}

Here's what the quantity input currently looks like;

enter image description here

And this is how I want it to look;

enter image description here

My code above will make this change but it causes an error on the basket page

Upvotes: 1

Views: 1010

Answers (1)

Keylies
Keylies

Reputation: 195

You must override the WooCommerce cart template to change this input, as explained here : https://docs.woocommerce.com/document/template-structure/#section-1

You will find this input code in cart.php, located in plugins/woocommerce/templates/cart/cart.php. Copy this template and paste it in a woocommerce directory at the root of your theme.

Upvotes: 1

Related Questions