Display products randomly according to category

0

I'm trying to change the display of the products to random when only when the category is the child's parent did the following code:

$id = 2;
$cat = Mage::getModel('catalog/category')->load($id);
$subcats = $cat->getChildren();
$categoria = $_productCollection->getCategoryIds();
foreach(explode(',',$subcats) as $subCatid){
     $_category = Mage::getModel('catalog/category')->load($subCatid);
    if($_category->getIsActive() and $categoria = $_category ){
     $_productCollection=$this->getLoadedProductCollection();
    }
}

There is a lot of error :( who can help thank you.

    
asked by anonymous 12.09.2014 / 21:56

2 answers

1

I did it!

$current_id=  Mage::getModel('catalog/layer')->getCurrentCategory()->getParentCategory()->getId();/Pega a id do pai da categoria selecionada */

$current_id2=  Mage::getModel('catalog/layer')->getCurrentCategory()->getId();//pega a id da categoria selecionada

$catPrincipal = 2;
$cat = Mage::getModel('catalog/category')->load($catPrincipal);/* Carrega categoria pelo id */
$subcats = $cat->getChildren();/*Retorna os Ids separados por virgula*/
$x = 0;
$catFilhas = array();/* Array com as filhas*/
foreach(explode(',',$subcats) as $subCatid){
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()){
        $catFilhas[$x] = $_category->getId();
    }
    $x++;
}

if($current_id == $catPrincipal){// Checa se a categoria é filha da categoria principal
    $_productCollection = Mage::getResourceModel('catalog/product_collection')
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addCategoryFilter(Mage::getModel('catalog/category')->load($current_id2))
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->joinField(
            'is_in_stock',
            'cataloginventory/stock_item',
            'is_in_stock',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left'
        )
        ->addAttributeToFilter('is_in_stock', array('eq' => 1))
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('visibility', 4)
        ->setPageSize(8);
        $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
}else{
    foreach($catFilhas as $values){
        if($current_id == $values){//Checa se é uma subcategoria das categorias filhas da principal
            $_productCollection = Mage::getResourceModel('catalog/product_collection')
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addCategoryFilter(Mage::getModel('catalog/category')->load($current_id2))
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->joinField(
                'is_in_stock',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left'
            )
            ->addAttributeToFilter('is_in_stock', array('eq' => 1))
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter('visibility', 4)
            ->setPageSize(8);
            $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
            break;
        }
    }
        if(!isset($_productCollection)){// Se não cair em nenhuma das duas ultimas condições
            $_productCollection=$this->getLoadedProductCollection(); 
        }
}

$_helper = $this->helper('catalog/output');
    
23.09.2014 / 22:55
1

You can use something like this:

{{block type="catalog/product_list_random" name="home.catalog.product.featured" category_id="60" columnCount="2" num_products="4" template="catalog/product/featured.phtml"}}

I'll try to explain a little more:

Here we load the catalog type ramdom:

type="catalog/product_list_random"

Category to be filtered:

category_id="60" 

Number of columns, not required:

columnCount="2" 

Quantity of products to be listed:

num_products="4"

Template template to be used for display:

template="catalog/product/featured.phtml"

In the list file you need to add the specific category filter as below:

<?php

$categoryid = 12;

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');

foreach ($collection as $_product) { ?>

<a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

<?php } ?>
    
12.09.2014 / 22:28