1

I have this code, that return collection to my phtml template - work perfectly:

class Namespace_Tabs_Block_Content extends Mage_Core_Block_Template

public function getProducts()
{
    $collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('*')

                    ->addAttributeToFilter('color', array('in' => array(27, 26, 20)))

                    ->addFieldToFilter('status','1')
                    ->addFieldToFilter('visibility', array('in' => array(2,4)));

    return $collection;
}

and this part of phtml :

<?php $_items = $this->getProducts() ?>

<div class="block">

    <?php foreach ($_items as $_item): ?>
        <div class="product">
            <a href="<?php echo $_item->getProductUrl() ?>" title="
                <?php echo $this->htmlEscape($_item->getName()) ?>"
            </a>
        </div>
    <?php endforeach; ?>

</div>

but when im trying to display each collection for color attribute - my inner foreeach shows nothink. this code, where somethink wrong

public function getProducts()
{
    $collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('*');

    $productsByColor = array('red' => $collection->addAttributeToFilter('color', array('eq' => 26)),
                             'blue' => $collection->addAttributeToFilter('color', array('eq' => 27)),
                             'black' => $collection->addAttributeToFilter('color', array('eq' => 20)),
                            );
    return $productsByColor;
}

phtml

<?php $products = $this->getProducts() ?>
<div class="block container">
<?php foreach ($products as $color => $_items): ?>

    <div class="block-content table" id="<?php echo $color ?>" >
        <div class="tab"> <?php echo $color ?> </div>
        <?php foreach ($_items as $_item): ?>
           ... nothink here ...
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>
</div>
0

$collection is an object so it is passed by reference. So when you call addAttributeToFilter on the second row (for blue) you are adding a filter to the same collection you used on the first row (for red).
So in the end you get something that translates into SQL like this:

where color = 'red' and color = 'blue' and color = 'black'

Obviously you are going to get the a blank result.
So make your code look like this:

public function getProducts()
{
    $collection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('*');

    $productsByColor = array();
    $red = clone $collection;
    $red->addAttributeToFilter('color', array('eq' => 26));
    $productsByColor['red'] = $red;

    $blue = clone $collection;
    $blue->addAttributeToFilter('color', array('eq' => 27));
    $productsByColor['blue'] = $blue;

    $black = clone $collection;
    $black->addAttributeToFilter('color', array('eq' => 20));
    $productsByColor['black'] = $black;
    return $productsByColor;
}

[EDIT]
Or try this other approach

public function getProducts()
{
    $config = array('red' => 26, 'blue' => 27, 'black' => 20);
    $productsByColor = array();
    foreach ($config as $key => $value) {
         $productsByColor[$key] = Mage::getModel('catalog/product')->getCollection()
                   ->addAttributeToSelect('*')
                   ->addAttributeToFilter('color', $value);
    }
    return $productsByColor;
}
  • Thank you, @Marius, for ur reply. good solution, already copied, but got this magento error : You cannot define a correlation name 'at_color' more than once – Саша Балаганов Nov 5 '15 at 15:19
  • See my update. I have an alternative – Marius Nov 5 '15 at 15:25

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.