Displaying Quantity Box on Magento Products Listing Page
As a newcomer to the world of Magento, you’re right in seeking an intuitive way for customers to modify product quantity directly from the category view page. Adding a quantity box allows users to quickly adjust their desired amount before finalizing the purchase.
Understanding the Default Behavior
By default, Magento doesn’t include a quantity box on the category listing page. Instead, it redirects you to the individual product detail page where you can then specify the quantity before adding the item to your cart.
This is done for performance reasonsâhaving a single quantity input field per product reduces server load compared to having one for each item in a category.
Implementing a Quantity Box: The Right Approach
While Magento’s default approach has its merits, it also creates a poor user experience, especially if you have many products on the same page. Users may need to click through several pages just to adjust quantities.
To provide a seamless shopping experience, we’ll add a quantity box directly to each product listing using an observer and custom phtml template.
Step-by-Step Guide
1. Create a Custom Phtml Template
Navigate to your theme folder (usually located at app/design/frontend/[Your_Theme]/template/catalog/product/list.phtml).
Create a new file named product_list_quantity.phtml. This file will hold our custom quantity box logic.
<!-- app/design/frontend/[Your_Theme]/template/catalog/product/list.phtml -->
<div class="quantity">
<label for="qty">Quantity:</label>
<input type="text" name="qty" id="qty" value="1" min="1" size="2"/>
</div>
2. Add Quantity Box to the Phtml File
Open your original list.phtml file and find where you want to place the quantity box (usually right before the “Add to Cart” button).
Insert the contents of our new product_list_quantity.phtml file at this location.
The final combined list.phtml should look something like this:
<!-- Combined app/design/frontend/[Your_Theme]/template/catalog/product/list.phtml -->
...
<div class="item-actions">
<div class="quantity">
<label for="qty">Quantity:</label>
<input type="text" name="qty" id="qty" value="1" min="1" size="2"/>
</div>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
</div>
...
3. Update the Product Controller
Now we need to make sure that when a user submits their quantity, it gets added to the cart correctly.
Find and open app/code/core/Mage/Checkout/controllers/CartController.php.
Around line 208, you’ll find the following code block:
if ($params = $this->getRequest()->getParams()) {
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized([
'locale' => Mage::app()->getLocale()->getLocaleCode()
]);
foreach ($params['qty'] as $sku => $qty) {
...
Add the following line just above the foreach loop:
$filter->setAllowDecimal(false);
This ensures that only integer values are accepted for the quantity field.
Verifying Your Changes
Clear your cache (System > Cache Management > Flush Magento Cache).
Visit a category page on your storefront. You should now see a quantity box next to each product’s “Add to Cart” button, allowing customers to adjust quantities directly from the listing page.
Conclusion
Adding a quantity box to Magento’s product listing pages significantly improves user experience by streamlining the buying process. By following this guide, you’ve learned how to override core templates and customize them to meet your specific business needs.
Remember that customizing core files like we did here is generally discouraged due to upgrade issues. For production sites, consider using a module extension or a third-party plugin from Magento Marketplace for a more robust solution.
For further assistance with Magento development and other e-commerce challenges, feel free to explore the resources available at Laravel Company. We offer expert consultation services tailored to help businesses like yours maximize their online potential.