0

On my website home page, I added new products using block in that add to cart button is not adding products to cart only the first time when I clear the cache but if I open the site in a new browser and add to cart I get the (shopping cart is empty) page, which is very strange.

Here is the code for new.phtml

<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="page-title category-title">
        <h1><?php echo $this->__('New Products') ?></h1>
</div>
<?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_products->getItems() as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid row">
        <?php endif ?>
            <li class="item span3<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <div class="extra-block">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
                </div>

                    <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
                    <p class="desc_grid"><?php $small_getDescription = strip_tags($_product->getShortDescription());
         if (strlen($small_getDescription) < 80) echo($small_getDescription);
         else { echo substr($small_getDescription, 0, 60).'...';} ?></p> 
         <?php // echo $this->getPriceHtml($_product, true, '-new') ?>
                    <div class="clear"></div>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                    <div class="product-shop">

                  <?php if($_product->isSaleable()): ?>
                            <button type="button" title="<?php echo $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>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('COMING SOON') ?></span></p>
                        <?php endif; ?>

                <?php /*
                    <div class="actions">


                        <ul class="add-to-links">
                            <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                <li><a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <?php endif; ?>
                            <?php if ($_compareUrl = $this->getAddToCompareUrl($_product)): ?>
                                <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                            <?php endif; ?>
                        </ul> 
                    </div> 
                    */?>
                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
        </ul>
        <?php endif ?>
    <?php endforeach; ?>
<?php endif; ?>
0

the problem is with new security added in magento 1.9 and form_key that is added to add to cart buttons and will not cache the New Products Block. Put cache_lifetime="1" at the end of the calling block in CMS page, this will disable the caching for the new products

<action method="setCacheLifetime">
        <lifetime>1</lifetime> 
    </action>

or

{{block type="catalog/product_new" column_count="6" products_count="12" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml" cache_lifetime="1"}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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