0

How do I check if an attribute value is assigned to any product? I have a product attribute A with values aa, bb, cc, dd, and I want to know if any product has aa or bb or cc or dd attribute value.

i.e if count(aa) in product attribute table >0

MY code so far

/*Get the Attributes for cat id 9 */
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory(Mage::getModel('catalog/category')->load(9));
$validAttributes = array();
foreach ($layer->getFilterableAttributes() as $attribute) {
    //allow only select attributes - you can implement your additional filters here
    if ($attribute->getFrontendInput() == 'select'){
        $validAttributes[] = $attribute;
    }
}

$i=0;
$name='';
/*Create the menu html in loop*/
foreach ($validAttributes as $attribute) {

    $options = $attribute->getSource()->getAllOptions();
    $attr=$attribute->getData();
    $name = $attr['frontend_label'];

    $temp.= '<li class=" nav-item level0 nav-1 level-top  level-top first last nav-item--parent classic nav-item--only-subcategories parent">
    <a href="#" class="level-top"> 
        <span>'.$name.'</span>
        <span class="caret">&nbsp;</span> 
    </a>
    <span class="opener"></span><ul class="level0 nav-submenu nav-panel--dropdown nav-panel" style="left: 44px; top: 40px; display: none;">';




    foreach ($options as $option)
    {
            //Here I want to check count($option['value']) in product >0
        $temp.='<li class="nav-item level1 nav-1-1 first last classic" >
                <a href="'.$catUrl.'?'.$attr['attribute_code'].'='.$option['value'].'">
                    <span> '.$option['label'].'</span>
                </a></li>';


    }
    $temp.= '</ul></li>';
    $i++;

}
jh314
  • 27,144
  • 16
  • 62
  • 82
Parangan
  • 148
  • 1
  • 15
  • possible duplicate of [Magento - Retrieve products with a specific attribute value](http://stackoverflow.com/questions/1332742/magento-retrieve-products-with-a-specific-attribute-value) – dmanners Apr 22 '15 at 12:52

3 Answers3

0

Use this code simple and efficient

$prod_ids_array=Mage::getModel('catalog/product')->getCollection()
                              ->addAttributeToFilter('custom_attribute_code',array('notnull' => true))
                              ->getAllIds();
print_r($prod_ids_array);

You will get product ids which have attribute value.

  • I want it to check by attribute value also like WHERE attribute_code = 7 and value ='canvas' and it shoul return the product count or ids – Parangan Apr 23 '15 at 04:51
0

You can easily get it by running a sql query.

I suppose you have attribute id = 74 and attribute type is integer

If it's integer so data will be stored in catalog_product_entity_int

select entity_id from catalog_product_entity_int where attribute_id = 73 and store_id = 0

so now you have the list of ids you are looking for.

phanvugiap
  • 336
  • 3
  • 8
  • I want it to check by attribute value also like WHERE attribute_code = 7 and value ='canvas' and it shoul return the product count or ids – Parangan Apr 23 '15 at 04:52
0

I know I'm late to the party but here's a query in case you would like to search by attribute_code instead:

SELECT
  cpe1.sku,
  cpe1.value
FROM
  catalog_product_entity_int cpei
  LEFT JOIN eav_entity_attribute eav ON eav.entity_attribute_id = cpei.attribute_id
  LEFT JOIN catalog_product_entity cpe1 ON cpe1.row_id = cpei.row_id
  LEFT JOIN eav_attribute ea ON ea.attribute_id = cpei.attribute_id
WHERE
  ea.attribute_code = "my_attribute_code"
  AND cpe1.attribute_set_id IS NOT NULL;

This only displays Integer attributes “catalog_product_entity_int” there are other tables too depending on your attribute type:

  • catalog_product_entity_datetime
  • catalog_product_entity_decimal
  • catalog_product_entity_gallery
  • catalog_product_entity_int
  • catalog_product_entity_media_gallery
  • catalog_product_entity_text
  • catalog_product_entity_tier_price
  • catalog_product_entity_varchar
Chris Rogers
  • 1,525
  • 2
  • 20
  • 40