1

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.

1 Answer 1

1

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.

7
  • OKay what about the quantity functionality? I want to add also the input with amount. Thank you very much for your help.
    – Navitua
    Commented Jan 11, 2016 at 14:16
  • I'll give you a example, hold on. Commented Jan 11, 2016 at 14:28
  • Did you get it? Commented Jan 12, 2016 at 15:02
  • Great Denis its working. I used your form with input field and button and all is perfect. Thank you very much for your help!
    – Navitua
    Commented Jan 13, 2016 at 14:28
  • You're welcome. If you think this answer deserves a upvote, give your upvote :) hahaha. Commented Jan 13, 2016 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.