How to check if attribute exist in product attribute set? Magento

18,597

Solution 1

EDIT: this is not the correct answer.

$product->offsetExists('pricekg');

See Varien_Object::offsetExists() (link).

Solution 2

now I'll provide an answer which works regardless!

$product = Mage::getModel('catalog/product')->load(16);

$eavConfig = Mage::getModel('eav/config');
/* @var $eavConfig Mage_Eav_Model_Config */

$attributes = $eavConfig->getEntityAttributeCodes(
    Mage_Catalog_Model_Product::ENTITY,
    $product
);

if (in_array('pricekg',$attributes)) {
    // your logic
}

Solution 3

To check if a specific attribute exists in product, it should return true even if the attribute has the value 'null'.

One way that works is:

$attr = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product',$code);
if (null!==$attr->getId())

{ //attribute exists code here }

It can also of course be written in one line:

if(null!===Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product','attributecode_to_look_for')->getId()) {
    //'attributecode_to_look_for' exists code here
}

Found it and modified a bit on: https://github.com/astorm/Pulsestorm/issues/3

Share:
18,597
user1992779
Author by

user1992779

Updated on June 24, 2022

Comments

  • user1992779
    user1992779 almost 2 years

    How to check if attribute exist in product attribute set?

    I need to know if a product has an attribute for its set of attributes.

    I get the attribute with:

    $attrPricekg = Mage::getModel('catalog/product')->load($_product->getId())->getPricekg();
    

    If attribut exist in product attribute set, $attrPricekg display: set value for the product or 0 if no value set for the product.

    If the attribute does not exist in product attribute set, $attrPricekg display 0. This is my problem.. I need to avoid this, I want to check that the attribute does not exist for that product.

    Thanks.

  • user1992779
    user1992779 over 11 years
    The first solution doesn't work correctly. I need to check for specific product.
  • freento
    freento over 11 years
    Try to do $attribute = Mage::getModel('catalog/product')->load(1)->getResource()->g‌​etAttribute($attribu‌​teCode);
  • freento
    freento over 11 years
    also you may try $attributes = $product->getAttributes();
  • user1992779
    user1992779 over 11 years
    Thanks. :) $attributes = $product->getAttributes() and foreach construct helped me!!!
  • beeplogic
    beeplogic over 11 years
    Will this only return true if there is a value for that attribute? Or does magento put in a key in $_data with a NULL value when there is no record in the product_entity_attribute_[type] tables?
  • R T
    R T about 10 years
    this one is general and overall perfect. i needed that to remove an attribute only if it exists. to remove existing attrib $installer->removeAttribute('catalog_product', 'attributecode_to_look_for'); simple.
  • Zsolti
    Zsolti over 8 years
    Thanks for the answer. After adding a closing bracket to the condition of the first example it works!