1

We want to use featured products on specified categories.

It works if we set the attribute featured to yes of a product if it is in the same category. But what we want to achieve is to show the products from anchor categories as well.

I think it's pretty simple, but I'm not able to get it work

For this we have a file featured-products.phtml with the below code.

<?php
$_categoryId = $this->getCategoryId();

$_productCollection = Mage::getModel('catalog/category')->load($_categoryId)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('inchoo_featured_product', array('eq' => 1))
    ->setOrder('price', 'ASC');
?>

<div class="featured-product">
    <h2><?php echo $this->__( $this->getLabel() ); ?></h2>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col">
        <?php $i=0; foreach ($_productCollection as $_product): ?>
            <?php /*if ($i++%$_columnCount==0): ?>
            <?php endif*/ ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                    <?php $_imgSize = 210; ?>
                    <img id="product-collection-image-<?php echo $_product->getId(); ?>"
                         src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($_imgSize); ?>"
                         alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
                </a>
                <div class="product-info">
                    <?php if($_product->getRatingSummary()): ?>
                        <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                    <?php else : ?>
                    <div class="ratings">
                        <div class="rating-box">
                            <div class="rating" style="width:0%"></div>
                        </div>
                    </div>
<!--
                        <div class="ratings">
                        <div class="no-rating">&nbsp;</div>
                        </div>
-->
                    <?php endif; ?>

                    <?php $_shipping_costs = $_product->getAttributeText('shipping_costs'); ?>
                    <?php $qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>

                    <?php if ($_shipping_costs == '0.00' && $qty >= 1 || $_shipping_costs == '0.00' && $_product->isConfigurable()) : ?>
                        <div class="stock-status">
                            <span class="in-stock"><i class="fa fa-truck" aria-hidden="true"></i> <strong>Gratis</strong> verzending</span>
                        </div>

                    <?php elseif ($_shipping_costs == '30.00' && $qty >= 1 || $_shipping_costs == '30.00' && $_product->isConfigurable()) : ?>
                        <div class="stock-status">
                            <span class="in-stock"><i class="fa fa-truck" aria-hidden="true"></i> Bezorging <strong>&euro; 30,-</strong></span>
                        </div>

                    <?php elseif ($_shipping_costs == '45.00' && $qty >= 1 || $_shipping_costs == '45.00' && $_product->isConfigurable()) : ?>
                        <div class="stock-status">
                            <span class="in-stock"><i class="fa fa-truck" aria-hidden="true"></i> Bezorging <strong>&euro; 45,-</strong></span>
                        </div>
                    <?php elseif ($qty <= 0 && !$_product->isConfigurable()) : ?>
                        <div class="stock-status">
                            <span class="not-in-stock"><i class="fa fa-truck"></i> <?php echo $this->__('Out of stock!'); ?></span>
                        </div>
                    <?php else : ?>
                        <div class="stock-status">
                            <span>&nbsp;</span>
                        </div>
                    <?php endif;?>

                    <div class="clearfix"></div>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h2>
                    <?php
                    // Provides extra blocks on which to hang some features for products in the list
                    // Features providing UI elements targeting this block will display directly below the product name
                    //if ($this->getChild('name.after')) {
                    //    $_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
                    //    foreach ($_nameAfterChildren as $_nameAfterChildName) {
                    //        $_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
                    //        $_nameAfterChild->setProduct($_product);
                    //        echo $_nameAfterChild->toHtml();
                    //    }
                    // }
                    ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                </div>
            </li>
            <?php /*if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
            <?php endif*/ ?>
        <?php endforeach ?>
    </ul>
    </div>
    <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>

And we load it via a static block with the code below

{{block type="core/template" category_id="1666" label="Uitgelichte producten" template="catalog/product/featured-products.phtml"}}
  • anchor categories ??? you want to show featured products in parents category as well ? – Gopal Patel Dec 9 '16 at 11:35
  • No the child category – Wouter Dec 9 '16 at 12:24
  • means In parent category you also want to show featured products of child categories – Gopal Patel Dec 9 '16 at 12:31
  • Yes! Exactly that is what im looking for – Wouter Dec 9 '16 at 12:45
  • if your current category is anchor then getcollection will load all products of the child category also – Gopal Patel Dec 9 '16 at 13:00

Your Answer

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

Browse other questions tagged or ask your own question.