Magento 1.9 – How to Filter Products by Category and Attributes

attributescategory-productsdatabasefiltermagento-1.9

I have searched about how to filter and I have tested about 20 codes but always return "".
The situation is the following:
– For the current category, I have 8 attributes, 7 are multiselect checkbox and the last one is radio button.
For example:
Attribute code: eniso20345_2011. The selected checkbox value for this one is "S1" and "S3". The current category is "shoes".

So I want to get all products which have the attribute eniso20345_2011 = ("S1" and "S3") into the category "shoes". How can I do that?

This code run but I do not want information about one specific sku, I want all SKU which have specific attributes values and specific category.

$productsS1 = Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToFilter('sku',"CUMADE000036")
            // ->addAttributeToFilter('eniso20345_2011', 'S1')
            ->load();

The idea is the commented line but this line return empty.

Thanks,

SQL Return:
SELECT e.*, at_category_id.category_id, at_eniso20345_2011.value AS eniso20345_2011 FROM catalog_product_entity AS e LEFT JOIN catalog_category_product AS at_category_id ON (at_category_id.product_id=e.entity_id) INNER JOIN catalog_product_entity_varchar AS at_eniso20345_2011 ON (at_eniso20345_2011.entity_id = e.entity_id) AND (at_eniso20345_2011.attribute_id = '207') AND (at_eniso20345_2011.store_id = 0) WHERE (at_category_id.category_id IN(525)) AND (((FIND_IN_SET('S1', at_eniso20345_2011.value))))

SOLUTION

Well, this is the way for get all products which belongs to determined category and have determined value or values for determined attribute or attributes:

    $products = Mage::getModel('catalog/category')
    ->load($categoryId)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(
    array(array('attribute'=>'eniso20345_2011', 'finset'=>1933)))
    ->joinField(
        'category_id',
        'catalog/category_product',
        'category_id',
        'product_id=entity_id',
        null,
        'left'
    );
    echo '<br>SIZE : '. $products->getSize() . PHP_EOL; 

* The finset value is the Id of the option, if the option has like value 'S1', you have to use the ID associated to 'S1'.

I hope that this can help everyone with doubts about this.

Best Answer

The following code will filter all the products/skus with value S1 for attribute eniso20345_2011 under the shoes category

    $yourShoesCategoryId = 22; // replace it with your category id

    $_productCollection =   Mage::getResourceModel('catalog/product_collection')
    ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
    ->addAttributeToFilter('category_id', array('in' => $yourShoesCategoryId))
    ->addAttributeToFilter(
        array(
              array('attribute'=>'eniso20345_2011', 'finset'=>array('S1')),
              array('attribute'=>'eniso20345_2011', 'finset'=>array('S2')),
        )
    )
    ->addAttributeToSelect('*');

    $_productCollection->load();

     echo 'SIZE : '. $_productCollection->getSize() . PHP_EOL; 

     echo 'SQL : '. $_productCollection->getSelect()->__toString() . PHP_EOL;

Update #2

$someAttribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'eniso20345_2011');

$_productCollection =   Mage::getResourceModel('catalog/product_collection')
    ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
        ->addAttributeToFilter('category_id', array('in' => $yourShoesCategoryId))
        ->addAttributeToFilter($someAttribute,
            array(
                array('finset'=> array('S1')),
                array('finset'=> array('S2')),
            )
        )
        ->addAttributeToSelect('*');
Related Topic