1

How can I show featured products from a certain category only?

I managed to create 'featured product' as one of the attributes for my products and I have 'tagged' a few products to this attribute. But my problem is I only want to show the featured product from one particular category only.

I have 'wine', 'food', 'gifts', etc as my categories. And each category has their own featured products, so when I am on wine category view page, I want to show the featured ones from wine category only, and then I want to show the ones from food category only when I am on food category view page.

I followed the guides here to create 'featured product' attribute,

1. http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/how_to_create_a_featured_product

2. http://www.magentocommerce.com/boards/viewthread/84852/

1 Answer 1

3

Source: https://stackoverflow.com/a/12288000/158325


Found a working solution using Alan Storm's advice.

/template/custom/featured-product.phtml

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

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

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

    <?php foreach ($_productCollection as $_product): ?>
        <div class="item">
            <a class="product-image" href="<?php echo $_product->getProductUrl() ?>">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
            </a>

            <a class="product-name" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>

            <?php echo $this->getPriceHtml($_product, true) ?>
        </div>

        <?php
        // Note: Exit after first product.
        break;
        ?>
    <?php endforeach ?>
</div>

In short, the collection is manually generated rather than receiving a collection (as my initial attempt did):

<?php $_productCollection = $this->getLoadedProductCollection() ?>
<?php $_collectionSize = $_productCollection->getSize(); ?>


Using in a CMS Page:

{{block type="core/template" category_id="13" label="Product of the Month" template="custom/featured-product.phtml" }}


Using in a template:

/layout/local.xml

<default>
    <reference name="footer">
        <block type="core/template" name="custom.featuredProduct" as="featured_product" template="custom/featured-product.phtml">
            <action method="setData"><key>category_id</key><value>13</value></action>
            <action method="setData"><key>label</key><value>Product of the Month</value></action>
        </block>
    </reference>
</default>

/template/page/html/footer.phtml

<?php echo $this->getChildHtml('featured_product') ?>


Helpful resources:

How to get a product collection:

Using magic getters/setters:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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