2

I am building a carousel for products I have all the relevant information e.g name price image but I need an add to cart button for each one of these currently I have got an add to cart button for them all but it adds the main product to the basket for whatever product page I am on. How do I achieve something which adds the relevant product to cart. See my code below.

<?php

$category_id = 577; // if you know static category then enter number

$catagory_model = Mage::getModel('catalog/category')->load    ($category_id); //where $category_id is the id of the category



 $collection = Mage::getResourceModel('catalog/product_collection');

    $collection->addCategoryFilter($catagory_model); //category filter

    $collection->addAttributeToFilter('status',1); //only enabled product

    $collection->addAttributeToSelect(array('name','url','small_image','price')); //add product attribute to be fetched

    //$collection->getSelect()->order('rand()'); //uncomment to get products in random order    

    $collection->addStoreFilter();          

    if(!empty($collection))

    {

            foreach ($collection as $_product):

        echo "<div class = 'item'>";
    echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150,150) . '"/>';
           echo "<span class ='prod_name'>";
            echo $_product->getName();   //get product name 
           echo "</span>";  
           echo "<span class = 'prod_price'> ";
            $getPrice =  "£" . $_product->getPrice();

           $snip_string = substr($getPrice , 0 , -2); 
           echo $snip_string;
           echo '</span>';  

           echo "<button type = 'button' title='Add to Cart' id = 'product-addtocart-button' class = 'button btn-cart'
           onclick = 'productAddToCartForm.submit(this)'>";
           echo '<span>';
           echo '<span>';
             echo 'Add to Cart';
           echo '</span>';
           echo '</span>';
           echo '</button>';  

        echo '</div>';    

        endforeach;


    }else

        {

            echo 'No products exists';

    }              

?>
0

3 Answers 3

2

simply you can get add to cart url just passing the product object

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

Use below code :

<button type="button" title="<?php echo $this->quoteEscape($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>
1
  • How can i solve the add to cart button error, justpaste.it/6fi1f Your code also not working.
    – Gem
    Aug 9, 2018 at 6:04
0
<?php foreach ($collection as $_product): ?>

<div class = 'item'>
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150,150)?>" />
<span class ='prod_name'>
<?php echo $_product->getName();   //get product name ?>
</span>
<span class = 'prod_price'>
    <?php 
    $getPrice =  "£" . $_product->getPrice();
    $snip_string = substr($getPrice , 0 , -2); 
    echo $snip_string;
    ?>

</span>
<button type = 'button' title='Add to Cart' id = 'product-addtocart-button' class = 'button btn-cart'
   onclick = "setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
<span><span>Add to Cart</span></span></button>';  

</div>
<?php endforeach; ?>
1
  • How can i solve the add to cart button error, justpaste.it/6fi1f Your code also not working.
    – Gem
    Aug 9, 2018 at 6:04

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.