0
votes

I have a custom landing page on my main category pages where i want to show a random selection of 9 products using a collection. When i had my category setup like below with the main category anchor Yes and subcategories No it works ok and displays the random 9 products

Main category A - anchor Yes
 - sub category 1 - anchor No
 - sub category 2 - anchor No
 - sub category 3 - anchor No

But I now need to switch this around and have the main category as NO and sub categories as anchor Yes nothing gets displayed and the collection is empty

Main category A - anchor No
 - sub category 1 - anchor Yes
 - sub category 2 - anchor Yes
 - sub category 3 - anchor Yes

My code is:

$_category  = $this->getCurrentCategory();
$collection = $_category->getProductCollection();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = 9;
$collection->setPage(1, $numProducts)->load();

foreach($collection as $product) {
    // display...

Is there a reason why the collection code won't work this way?

1
do var_dump($_category->getId()) and share the resultPankaj

1 Answers

0
votes

anchor type means that main category show all children from subcategories in your case you should get all subcategories and only after that to get random products