# Quantity Box on Shop Page Add to Cart Buttons

> Adds a quantity field next to the add to cart button for simple products in shop listings, so shoppers can add several at once without opening the product.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, shop, quantity, ajax
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-loop-quantity-input/

## Code

```php
add_filter( 'woocommerce_loop_add_to_cart_link', function ( $html, $product ) {
    if ( ! $product->is_type( 'simple' ) || ! $product->is_purchasable() || ! $product->is_in_stock() || $product->is_sold_individually() ) {
        return $html;
    }

    $qty = woocommerce_quantity_input( array(
        'min_value'   => $product->get_min_purchase_quantity(),
        'max_value'   => $product->get_max_purchase_quantity(),
        'input_value' => $product->get_min_purchase_quantity(),
        'input_name'  => 'quantity',
    ), $product, false );

    return '<div class="wpide-loop-qty" style="display:flex;gap:6px;align-items:center;justify-content:center;">' . $qty . $html . '</div>';
}, 10, 2 );

add_action( 'wp_footer', function () {
    if ( ! function_exists( 'is_woocommerce' ) ) {
        return;
    }
    ?>
<script>
document.addEventListener('change', function (e) {
    if (!e.target.matches('.wpide-loop-qty input.qty')) { return; }
    var button = e.target.closest('.wpide-loop-qty').querySelector('.add_to_cart_button');
    if (button) { button.setAttribute('data-quantity', e.target.value); }
});
</script>
    <?php
} );
```

## Notes

Works with AJAX add to cart. Variable products keep their "Select options" button.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Quantity Box on Shop Page Add to Cart Buttons" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
