Display products randomly according to category

Asked

Viewed 686 times

0

I am trying to change the display of the products to Random when only when the category is the daughter of the main I made 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 there thank.

2 answers

1


I got!

$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');

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 kind of ramdom catalog:

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 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 } ?>
  • 3

    A lot fish in his last answers, my dear Otto :( . . . And if I wanted to know why your code solves the problem?

  • 1

    Master @brasofilo tried to explain a little better the functioning, grateful for the criticism

  • guy already tried so the question is that this check has to be done inside the file list.phtml

  • updated the answer I hope to help you

  • I will test here thank you for your attention.

  • I updated there the expensive question I hope you now understand exactly what I’m trying to do.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.