2

I have spent two days in this problem: I can´t find the right solution for getting all products which are related to a specified category and which have the searched attribute.

In my special case: We got a selector for printer colors in three steps: 1. Choose the printer brand 2. Choose the device type (laser printer, inject printer, etc.) 3. Choose the modell type

The related printer colors should be shown now. So I need to find out which they are.

I tried like this:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category')->load(1274);
$prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);
$prodCollection->addAttributeToSelect('printercolor');
$prodCollection->addAttributeToFilter('printercolor', array('eq' => $currentPrinterDescription);

In the two days I have many alternative solutions for doing this, but none of them worked as accepted.

Hope you guys can help me.

Edit: I solved it myself. My code looks now like this:

$category = Mage::getModel('catalog/category')->load($categoryId);
$prodCollection = $category->getProductCollection()->addAttributeToSelect('my_attribute');
foreach ($prodCollection as $prodAttr)
{
    $var = explode(',', $prodAttr->getData('my_attribute'));
    if ($attr = $prodAttr->getResource()->getAttribute('my_attribute'))
    {
          $af = $attr->getFrontend();
          $selectedOptions = $af->getSelectOptions();
                  foreach ($selectedOptions as $s)
                  {
                        $products[] = trim($s['label']);
                        // ...
                  }
    }
}
// ...
0
2

Since multiselects are stored as comma separated values you can probably use a like on it.

$prodCollection->addAttributeToFilter('printercolor', array(
   array($currentPrinterDescription),
   array('like'=>'%,'.$currentPrinterDescription),
   array('like'=>$currentPrinterDescription.',%'),
   array('like'=>'%,'.$currentPrinterDescription.',%'),
));

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.