1

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.

0

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('*');
|improve this answer|||||
  • Thanks for your answer, however, this last echo returns 0. Really I can not understand how is so hard to code this easy query. If you have some idea, I wait for your answer. I am using Magento 1.9. – Javi Torre Sustaeta Feb 15 '19 at 9:01
  • The query returns no records, so you need to debug the query. as a starting point get your SQL query by adding the line below, echo 'SQL : '. $_productCollection->getSelect()->__toString() . PHP_EOL; which gives you an idea what's wrong with this query. Run this query against your database tables by removing each of the AND conditions one by one, so you can figure out which condition is the cause. – Haijerome Feb 15 '19 at 9:30
  • i've just updated my answer, as you are dealing with multiselect checkboxes attribute, the filter needs to be an array which should be inside another array. Check this query and let me know what you got. – Haijerome Feb 15 '19 at 9:39
  • updated also with an alternate SQL which could also be useful in this case. – Haijerome Feb 15 '19 at 9:53
  • I upload my question with the SQL that is generated by the code, and I have seen that is using the catalog_product_entity_varchar, but I have read during these days that the values for multiselect attributes options are managed by another table but I do not remember it name. Do yoy think it could be this? If it is, what I should to change? Thanks – Javi Torre Sustaeta Feb 15 '19 at 10:10

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.