0

I have two multi-select attributes in my Magento 1.9 install (with their respective options) like this:

make

  • Manufacturer A
  • Manufacturer B
  • Manufacturer C

model

  • Type A
  • Type B
  • Type C

I have some products that will have more than one "make" and "model".

How do you load all products from a specific category and filter by products having a specific selected multi-select attribute option in Magento?

I am trying to load products where a scenario like this exists:

make = [Manufacturer A] and [Manufacturer C]
model = [Type A] and [Type B]

This is what I have tried so far:

// Mini-config
$make_attribute_code = 'make';
$model_attribute_code = 'model';
$filter_by_make = 'Manufacturer A';
$filter_by_model = 'Type A';
$load_from_category = 10117;

// Parse attribute option id of "Make"
$make_option_id = null;
$makeAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $make_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($makeAttributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $attributeOption) {
    if (trim($attributeOption['label']) == $filter_by_make) {
        $make_option_id = (int)trim($attributeOption['value']);
        break;
    }
}

// Parse attribute option id of "Model"
$model_option_id = null;
$modelAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $model_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($modelAttributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $attributeOption) {
    if (trim($attributeOption['label']) == $filter_by_model) {
        $model_option_id = (int)trim($attributeOption['value']);
        break;
    }
}

// Load category products having selected "Make" & "Model"
$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'eq' => $make_option_id)))
    ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'eq' => $model_option_id)))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );

If the product has only one "make" and "model" option selected, it appears to be showing. Any ideas?

2

To apply multiple value filter, First you need to create an array for all your selected options.

$filter_by_make = array('Manufacturer A','Manufacturer B');

When you get IDs for selected values, create an array for selected values like,

foreach ($attributeOptions as $attributeOption) {
    if (in_array(trim($attributeOption['label']), $filter_by_make)) {
        $make_option_id[] = (int)trim($attributeOption['value']);
        break;
    }
}

Now in filter section, Use

$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(
    array(array('attribute'=>$make_attribute_code, 'finset'=>$make_option_id)))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );

Reference https://magento.stackexchange.com/a/14116/23825

  • Thank you. This did the trick for me: ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'finset' => array($make_option_id)))) and ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'finset' => array($model_option_id)))) – Latheesan Feb 7 '17 at 9:21

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.