简体   繁体   中英

Get price and Add to cart button Magento

I create a block where I display all the products from a category by its ID. At this moment I can see the title of the product and its image, but i need the (ADD TO CART) and the (PRICE). My code is:

<div class="freeProducts voucher code">
    <?php

    $categoryid = 64;

    $category = new Mage_Catalog_Model_Category();
    $category->load($categoryid);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');

    foreach ($collection as $_product) { ?>

    <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

    <?php echo $this->getPriceHtml($_item, true) ?>
    <?php } ?>
</div>

Updated button code:

Remove:

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

Add:

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $_product->getId() ?>/')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

$this->getAddToCartUrl($_item) will not work in custom code. This is used in product detail page where helper is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM