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

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.


ANSWERS:


EDIT: this is not the correct answer.

$product->offsetExists('pricekg');

See Varien_Object::offsetExists() (link).


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
}

I needed to check if a specific attribute existed in product, it should return true even if the attribute had the value 'null'.

One way that worked for me was

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

It can of course be written as a oneliner

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:


May be this way is better for you:

$attribute = Mage::getModel('catalog/product')->load($productId)->getResource()->getAttribute($attributeCode);
if ($attribute && $attribute->getId()) { ... }

Also you may try

$attributes = $product->getAttributes();

But you may check all in attribute collection:

$entityTypeId = Mage::getModel('eav/entity')
            ->setType('catalog_product')
            ->getTypeId();
$attributeId = 5;
$attributeSetName   = 'Default';
$attributeSetId     = Mage::getModel('eav/entity_attribute')
                ->getCollection()
                ->addFieldToFilter('entity_type_id', $entityTypeId)
                ->addFieldToFilter('attribute_set_name', $attributeSetName)
                ->addFieldToFilter('attribute_id', $attributeId)
                ->getFirstItem();

May be source code need some corrections, but I think you will understand the idea.

See some more examples here, also -



 MORE:


 ? Magento Catalog Product SubCategory Name
 ? Magento product grid image size
 ? HOW To Get Product Info Using REST API In Magento
 ? HOW To Get Product Info Using REST API In Magento
 ? HOW To Get Product Info Using REST API In Magento
 ? Magento Rest API Oauth URL Returning 404
 ? Magento oauth error for fetch product
 ? Create new magento Rest api to get category list in magento
 ? 401 Error "oauth_problem=nonce_used" Adding Products To Magento w/ Rest API
 ? Magento REST API /Product Call Returns Empty JSON Arrays