2

I am currently working on creating a product slider for promotional products.

I have created a new category within Magento called "Promotional" (ID = 497), and have assigned 8 products to it.

My issue is that my code (below) is displaying products from the wrong category and not the newly created "Promotional" category.

category-slider.phtml

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

if ($_products->getSize()):
    echo $_products->getSize(); 

    foreach ($_products as $_product): 

        echo $_helper->productAttribute($_product, $_product->getName(), 'name');

        echo "<pre>";
        print_r( $_product->debug() );
        echo "</pre>";

    endforeach; 
endif; 

Home page > Design

<reference name="footer_before">
    <block type="catalog/product_list" name="categoryslider" template="mytheme/page/html/sliders/category-slider.phtml" after="-">
        <action method="setCategory"><category_id>497</category_id></action>
    </block>
</reference>

I have tried reindexing as well as clearing all caches (they are all disabled anyway), yet products in a different category are being shown.

Any ideas?

  • try with this line instead: <block type="catalog/product_list" name="categoryslider" category_id="497" template="mytheme/page/html/sliders/category-slider.phtml" after="-"></block> – Anil Suthar Jun 3 '16 at 11:57
  • @AnilSuthar Unfortunately that doesn't work for me. I had to rework the XML slightly to use setCategoryId as suggested in the answer below. Maybe something else is going on, but I'm still confused! – wickywills Jun 3 '16 at 12:02
3

To me you should use setCategoryId instead of setCategory

The reason behind that is that the Mage_Catalog_Block_Product_List's _getProductCollection method is using the following code to load the category:

        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
                $this->addModelTags($category);
            }
        }
  • Thanks - this works. Slightly odd though, as I've managed to get setCategory working as I have above on another website. – wickywills Jun 3 '16 at 11:58

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.