0

I am trying to display a list of products on the home page. The list of products is the products with the attribute "promotion" with the dropdown value "yes". So far I have tried the following But it just blank and not actually displaying anything.:

I have re-used code from my template bought from Template Monster, the code was used to display the "New Products"


Edit 1: Partial Solution found.

Solution: On the 5th Line in promotion.phtml file, change $this->getProductCollection() to $this->_getProductCollection() . This solves the issue and starts displaying.

But, ideally I have not seen this thing causing issue



Current Code and Approach to solve this issue is as Below


  1. Created a Custom file in the Template called promotion.phtml in the folder app/design/frontend/default/template_name/template/catalog/product/.

Code of the file is as follows:

    <?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
    ?>

    <?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
        <div class="page-title category-title">
            <h1><?php echo $this->__('Promotional 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" id="new-carousel">
            <?php endif ?>
            <li class="item" itemscope itemtype="http://schema.org/product">
                <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(228) ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/></a>
                <div class="product-shop">
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                    <?php echo $this->getPriceHtml($_product, true, '-new') ?>
                    <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" itemprop="name">
                            <?php $product_name_carousel = strip_tags($_product->getName());
                            if (strlen($product_name_carousel) < 25) echo($product_name_carousel);
                            else { echo mb_substr($product_name_carousel, 0, 25,'UTF-8').'...';} ?>
                        </a></h3>
                    <div class="actions">
                        <div class="desc_grid" itemprop="description"><?php $small_getDescription = strip_tags($_product->getShortDescription());
                            if (strlen($small_getDescription) < 60) echo($small_getDescription);
                            else { echo mb_substr($small_getDescription, 0, 60,'UTF-8').'...';} ?></div>
                        <?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>
                            <span class="btn-separator"></span>
                            <button type="button" title="<?php echo $this->__('Details') ?>" class="button btn-details" onclick="setLocation('<?php echo $_product->getProductUrl() ?>')"><span><span><?php echo $this->__('Details') ?></span></span></button>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                        <!-- <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>
                <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
                <div class="label-product">
                    <?php if($_product->getData('new')){echo '<span class="new">'.$this->__('New').'</span>';  }?>
                    <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
                </div>
            </li>
            <?php if ($i%$_columnCount==0 || $i==count($_products)): ?>
                </ul>
                <div class="new-carousel-button">
                    <span class="carousel-pagination"></span>
                </div>
            <?php endif ?>
        <?php endforeach; ?>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery('#new-carousel').carouFredSel({
                    responsive: true,
                    width: '100%',
                    /* prev: '.carousel-prev',
                     next: '.carousel-next', */
                    scroll: 1,
                    auto : {
                        play : 1,
                        timeoutDuration :13000
                    },
                    items: {
                        visible: {
                            min: 1,
                            max: 5
                        },
                        width:234,
                        height:  'variable' //400
                    },
                    mousewheel: true,
                    swipe: {
                        onMouse: false,
                        onTouch: true
                    },
                    pagination: {
                        container: ".carousel-pagination",
                    }
                });
            });
        </script>
    <?php endif; ?>
  1. Added the following XML to the XML layout of the Home Page.

    <reference name="content">
        <block type="core/template" template="catalog/msrp/popup.phtml" name="product.tooltip"></block>
        <block type="catalog/product_list_promotion" name="home.catalog.product.promotion" alias="product_promotion" after="cms.wrapper" template="catalog/product/promotion.phtml">
            <action method="setColumnCount"><columns>30</columns></action>
            <action method="setProductsCount"><count>30</count></action>
            <action method="addPriceBlockType"><type>bundle</type><block>bundle/catalog_product_price</block><template>bundle/catalog/product/price.phtml</template></action>
            <action method="unsetData"><key>cache_lifetime</key></action>
            <action method="unsetData"><key>cache_tags</key></action>
        </block>
    </reference>
    
  2. The content of the File Promotion.php in the folder app/code/core/Mage/Catalog/Block/Product/List is as Follows:

    <?php
    
    class Mage_Catalog_Block_Product_List_Promotion extends Mage_Catalog_Block_Product_List
    {
        protected function _getProductCollection()
        {
            if (is_null($this->_productCollection)) {
                $collection = Mage::getResourceModel('catalog/product_collection');
                Mage::getModel('catalog/layer')->prepareProductCollection($collection);
    // your custom filter
                $collection->addAttributeToFilter('promotion', 1)
                    ->addStoreFilter();
    
                $this->_productCollection = $collection;
            }
            return $this->_productCollection;
        }
    }
    

Any pointers will be much appreciated. I am not a Magento Developer so working my way thru.

Many Thanks in Advance for your time.

  • what if in your Mage_Catalog_Block_Product_List_Promotion you log the collection sql Mage::log((string) $collection->getSelect()) after you add all your filters. – David Manners May 19 '15 at 8:31
  • @DavidManners thanks for the quick response. I would really appreciate if you can give a bit detailed info. Sorry, but quite a N00b to magento. – Maharshi Raval May 19 '15 at 8:35
1

Try to replace the _getProductCollection method with this

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $collection = Mage::getModel('catalog/collection')->getCollection();
        $collection->addAttributeToFilter('promotion', 1)
            ->addStoreFilter();
        $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite()
        $this->_productCollection = $collection;
    }
    return $this->_productCollection;
}

Also, if you have the flat catalog enabled, make sure the attribute promotion is marked as "Use in product listing". If it wasn't, then rebuild the indexes after setting it like that.

  • And then remove the line <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?> which is reloading every product again. This is slow and unnecessary with a properly configured collection (hint: your Template Monster theme is not a good one) – Fabian Schmengler Aug 31 '15 at 7:10

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.