Add to Cart with Quantity in Category List – Magento Guide

magento-1.7

I want to add add to cart to product category list.

What I have right now is:
1. I registered the addtocart in xml file catalog.xml

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
        <block type="catalog/product_list" name="product_list.addtocart" as="addtocart" template="catalog/product/list/addtocart.phtml"/>
</block>
  1. In template\catalog\product\list.phtml i added the call:

    <?php if($_product->isSaleable()): ?> <?php echo $this->getChildHtml('addtocart') ?> <?php endif ?>
    

What am I missing that i don't see the addtocart functionality with quantity?

I also moved the file addtocart.phtml to catalog/product/list/addtocart.phtml.

Best Answer

The addtocart.phtml file uses "catalog/product_view" type by default. Inside addtocart.phtml, the button "add to cart" use a javascript function to submit the product view form. So you can't use it on product list without some changes.

Simple approach

You can change your list.phtml just adding a button/link with a url to add to cart product:

$_url = Mage::helper('checkout/cart')->getAddUrl($_product);

Block approach

You can change your block "addtocart" and assign each product to it on list.phtml. Inside addtocart you can get url (like above) and build your button.

EDIT:

You can set a default qty in getAddUrl method by parameters:

<?php echo Mage::helper('checkout/cart')->getAddUrl($_product, array('qty' => 2)); ?>

If you want to use a input to qty, it's a quick example:

//My collection list loop (in list.phtml)
<?php $i=0; foreach ($_productCollection as $_product): ?>

    <form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>">
    // Code of my item product box here (I removed).
    // [...]
    <div class="actions">

        <?php if($_product->isSaleable()): ?>

            <div class="add-to-cart">
              <input type="hidden" name="id" class="prodId" value="<?php echo $_product->getId()?>"/>
              <input type="text" name="qty" id="qty" class="qty" maxlength="2" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>"/>
            </div>

            <a class="seemore" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('See More'); ?></a>
            <button type="submit" class="button" title="<?php echo $this->__('Add to Cart');?>"><span><?php echo $this->__('Add to cart'); ?></span></button>

        <?php else: ?>

            <p class="availability out-of-stock">
                <span><?php echo $this->__('Out of stock') ?></span>
            </p>

        <?php endif; ?>

    </div>
    // Rest of my list item. (I removed).
    // [...]
    </form>

<?php endforeach ?>

OBS: It's a simple example. This is not prepared to handle with configurable or grouped product.