Adding Quantity Option to Each Product Item in the Category List Page
Adding a quantity option to each product item on your category list page can enhance customer experience by allowing them to select the desired number of items before adding it to their cart. In Magento, this functionality is typically handled within the list.phtml file. Let’s explore how you can achieve this.
Understanding the Default Implementation
By default, the list.phtml file contains the logic for displaying an “Add to Cart” button for each product item:
<?php if($_product->isSaleable()): ?>
<p>
<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>
</p>
<?php else: ?>
<p class="availability out-of-stock">
<span><?php echo $this->__('Out of stock') ?></span>
</p>
<?php endif; ?>
This code checks if the product is saleable and displays either an “Add to Cart” button or an “Out of Stock” message.
Adding a Quantity Input Field
To add a quantity input field for each product, you can modify the list.phtml file as follows:
<?php if(!$_product->isGrouped()): ?>
<input type="text" name="qty" id="qty" placeholder="1" maxlength="12" value="<?php echo $_product->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty input-quantity" />
<?php endif; ?>
This code snippet checks if the product is not a grouped product and then displays an input field for quantity. The getProductDefaultQty() method is used to retrieve the default quantity value.
Solving the Quantity Display Issue
The current implementation may display a quantity of 0, which is incorrect. To fix this issue, you need to adjust the line of code inside the value attribute:
value="<?php echo $_product->getMinimalQty() * 1 ?>" /
By using the getMinimalQty() method instead of getProductDefaultQty(), you ensure that the minimum allowed quantity is displayed as the initial value in the input field.
Ensuring Proper Quantity Addition
When a customer enters a quantity and clicks the “Add to Cart” button, only 1 item is added to the cart. To resolve this issue, you need to modify the list.phtml file as follows:
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post">
<input type="text" name="qty" id="qty" placeholder="1" maxlength="12" value="<?php echo $_product->getMinimalQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty input-quantity" />
<button type="submit" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
</form>
By wrapping the “Add to Cart” button inside a form, you ensure that the entered quantity is submitted along with the product URL when the button is clicked. This allows the correct number of items to be added to the cart.
Conclusion
Adding a quantity option to your category list page can significantly enhance the shopping experience for your customers. By modifying the list.phtml file as described above, you can provide a user-friendly interface that allows customers to select the desired quantity before adding products to their carts.
Remember to test the implementation thoroughly on different browsers and devices to ensure compatibility and functionality. If you encounter any further issues or require more advanced customization options, consider consulting our team of Magento experts at LaravelCompany.com.