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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.