Magento: Add multiple add to cart buttons and qty fields to product view page

Magento: Add multiple add to cart buttons and qty fields to product view page

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Add multiple add to cart buttons and qty fields to product view page

Adding Multiple “Add to Cart” Buttons and Quantity Fields to Magento Product View Page

As an experienced developer, I understand your need to display multiple products on the view page, each with their own “Add to Cart” button and quantity field. While your current approach of filtering by product name is useful for grouping items, it doesn’t provide the functionality you require. Let’s dive into a more advanced solution.

The Challenge

The default Magento theme uses a single “Add to Cart” button for each individual product in the catalog. However, if you want to display multiple products with different options (like size or color), you need to modify the way the view page renders these items.

Approach: Extending the Product View Template

To achieve your goal, we’ll make modifications to the existing product/view.phtml template file located under app/design/frontend/base/default/template/catalog/product. Be sure to create a copy of this file in your theme directory (e.g., mytheme/template/catalog/product/view.phtml) to avoid overwriting core files.

Step 1: Modify the Product Collection

Before we start editing the template, let’s ensure that our product collection is set up correctly. In your controller or block class where you’re fetching the products, make sure you have something like this:

$collection = Mage::getModel('catalog/product')->getCollection();
$currentProduct = Mage::registry('current_product');
$collection->addAttributeToFilter('name', $currentProduct->getName());

This will fetch all products with the same name as the current product being viewed.

Step 2: Modify the Product View Template

Now, let’s modify the product/view.phtml file in your theme directory. We’ll replace the existing “Add to Cart” button logic with a custom loop that displays each product separately.

First, find this line of code:

<?php echo $this->getChildHtml('addtocart') ?>

Replace it with our custom loop:

<?php if (count($collection)): ?>
    <table>
        <?php foreach ($collection as $_item): ?>
            <tr>
                <td><?php echo $_item->getSku() ?></td>
                <td><?php echo $_item->getName() ?></td>
                <td>
                    <select name="qty_<?php echo $_item->getId() ?>" class="input-text qty">
                        <?php for ($i=1; $i<=5; $i++): ?>
                            <option value="<?php echo $i ?>"><?php echo $i ?></option>
                        <?php endfor; ?>
                    </select>

                    <button type="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')">Add to Cart</button>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
<?php endif; ?>

This code loops through each product in the collection, displaying its SKU and name. For each item, it creates a dropdown box for quantity selection and an “Add to Cart” button that points to the product’s URL.

Step 3: Add Custom JavaScript

To make the “Add to Cart” buttons work correctly with the standard Magento functionality, you’ll need to add some custom JavaScript. In your theme’s js directory, create a new file called addtocart.js.

require([
    'jquery',
    'mage/catalog-addtoCart'
], function($) {
    $('.product-add-to-cart button').off('click');
    $('.product-add-to-cart button').on('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('onclick').split("'")[1];
        setLocation(url);
    });
});

This JavaScript code listens for click events on the “Add to Cart” buttons and redirects the user to the product URL using Magento’s standard setLocation function.

Conclusion

With these modifications, you should now be able to display multiple products with different options on the view page, each with its own “Add to Cart” button and quantity field. This approach allows you to group similar items without losing the individual functionality of each product.

Remember, it’s important to test your changes thoroughly in a staging environment before deploying them to a live site. If you find yourself struggling with these modifications or any other Magento development tasks, feel free to reach out to our team at Laravel Company for expert assistance and consulting services.

Happy coding!

Checkout Catalog Themes

Our network