0

I have a problem with adding the add to cart button. Whenever I add the add to cart button the all of the sliders disapear from page. This is the example where I use the add to cart button .

    <h2>Compilations</h2>
    <div id="owl-demo6" class="owl-carousel">
    <?php
    $categoryId = 2; // this is the category holding your products  
      $products = Mage::getModel('catalog/product')->getCollection();
     $products->addAttributeToSelect(array(
                        'image',
                           'name',
                           'short_description',
                           'sku',
                           'asin'
           ))
           ->addFieldToFilter('visibility', array(
                           Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                       Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
           ));

//$products->load();
foreach ($products as $product)  : ?>
<div class="item">
<img src="<?php echo $localHelper->getImageUrl($product); ?>" alt="img"      /> 
 <?php if (!$product->getTypeInstance(true)->hasRequiredOptions($product)):?>
 <button type="button" title="<?php echo $this__('Add to Cart') ?>" 
class="button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($product); ?>')">
<span><span><?php echo $this->__('Add to Cart') ?></span></span></button>

<script>
jQuery.noConflict();
jQuery(document).ready(function() {

  jQuery("#owl-demo").owlCarousel({
    items : 4,
    lazyLoad : true,
    navigation : true
  }); 

If anyone could spot what I'm missing would be great. Thank you

6
  • This is custom page ? Commented Oct 13, 2015 at 10:54
  • This is in phtml file for my cms home page Commented Oct 13, 2015 at 10:55
  • if yes than that is because of line //$this->getAddToCartUrl($_product) ?> function getAddToCartUrl is not available in your block class Commented Oct 13, 2015 at 10:56
  • It there any other option to add this ? Commented Oct 13, 2015 at 10:57
  • it gives fatal error on page Commented Oct 13, 2015 at 10:57

2 Answers 2

1

Try This

<div id="owl-demo6" class="owl-carousel">
<?php
$categoryId = 2; // this is the category holding your products  
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect(array(
                            'image',
                               'name',
                               'short_description',
                               'sku',
                               'asin'
               ))
               ->addFieldToFilter('visibility', array(
                               Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                           Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
               ));

   //$products->load();
    foreach ($products as $product)  : ?>
    <div class="item">
    <img src="<?php echo $localHelper->getImageUrl($product); ?>" alt="img" /> 
 <?php if (!$product->getTypeInstance(true)->hasRequiredOptions($product)):?>
     <button type="button" title="<?php echo $this__('Add to Cart') ?>" 
  class="button btn-cart" onclick="setLocation('<?php echo  $this->helper('checkout/cart')->getAddUrl($product); ?>')">
    <span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
 <?php endif;?>
 </div>
  <?php endforeach;?>
</div>
19
  • It gives the same outcome as the one I have written.Which is there is no slider only the titles of <h2> </h2> that I haven't included on here. This means it reads the cms page just not the php part. Commented Oct 13, 2015 at 11:17
  • Try with Mage::helper('checkout/cart')->getAddUrl($product) Commented Oct 13, 2015 at 11:20
  • This doesn't work as well unfortunately <?php if (!$product->getTypeInstance(true)->hasRequiredOptions($product)):?> <button type="button" title="<?php echo $this__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php Mage::helper('checkout/cart')->getAddUrl($product) ?>')"> <span><span><?php echo $this->__('Add to Cart') ?></span></span></button> <?php endif;?> </div> <?php endforeach;?> </div> Commented Oct 13, 2015 at 11:28
  • add your full code Commented Oct 13, 2015 at 11:41
  • I have edited the code that is how the whole slider function looks Commented Oct 13, 2015 at 11:53
0

This due to $this of line "//$this->getAddToCartUrl($_product) ?>"

Call .phtml file from block and use this.

<?php $product = Mage::getModel('catalog/product')->load($this->getProduct()) ?>
<?php echo Mage::helper('checkout/cart')->getAddUrl($product) ?>

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.