0

I'm trying to select all products with an attribute called uitgelicht_product set to Ja. It's not the first time that I'm doing this and it usually goes well, but this is situation is a bit different, because I have 2 different attribute sets. The attribute occurs in both attribute sets: Default and Test.

I'm able to retrieve the products with uitgelicht_product set to Ja in the Default set, but I also want the products from the attribute set Test. I tried using this to see if I can get it only from Test:

$products->addAttributeToFilter('attribute_set_id','39');

But this doesn't return anything, while 39 is the attribute ID of Test. Changing 39 to 4 (the id of the Default set) works fine and returns the product using the attribute set Default with uitgelicht_product set to Ja.

How do I get the products with uitgelicht_product set to Ja in both the Test as the Default attribute set?

This is the entire code I'm using to retrieve the products:

<?php
$_helper = $this->helper('catalog/output');
$productBlock = $this->getLayout()->createBlock('catalog/product_price');

$yesOpID = null;
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'uitgelicht_product');
if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach ($options as $e) {
        if ($e['label'] == 'Ja'):
            $yesOpID = $e['value'];
        endif;
    }
}

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('uitgelicht_product', $yesOpID);
$products->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$products->addAttributeToFilter('attribute_set_id', '39');
$products->addStoreFilter();
$products->getSelect()->group('e.entity_id');
$products->getSelect()->order('RAND()');
$products->getSelect()->limit(4);
0

Please make your attribute uitgelicht_product with yes/no and then add in both attribute set and set some product yes. and please use below code.

<?php
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('uitgelicht_product','1');
$products->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$products->addAttributeToFilter('attribute_set_id','9');
//here 9 is my attribute set
print_r($products->getData());

?>
0

try

<?php
    $attribute_set_id = array('4','39');
    $products = Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToSelect('*');
                ->addAttributeToFilter('uitgelicht_product','Ja')
                ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                ->addFieldToFilter('attribute_set_id',array('in' => $attribute_set_id));

    print_r($products->getData());

?>
  • Unfortunately this didn't return anything. It returned: Array ( ) – user3478148 Aug 12 '16 at 13:56
  • check now i had changed code – Pradeep Sanku Aug 12 '16 at 13:59
  • Sorry, it still shows the same result. Could it be that it has something to do with a setting? – user3478148 Aug 12 '16 at 14:09

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.