Tag Archive: Magento cheat code


$productId = 10; 
$attributeName = 'my_attribute_name';

$product = Mage::getModel('catalog/product')->load($productId);
$attributes = $product->getAttributes();

$attributeValue = null;		
if(array_key_exists($attributeName , $attributes)){
	$attributesobj = $attributes["{$attributeName}"];
	$attributeValue = $attributesobj->getFrontend()->getValue($product);
}
echo $attributeValue; //attribute value for 'my_attribute_name'

Here is a code snippet exaple how you fetch all products by it’s attribute set name:-

//Fetch attribute set id by attribute set name
$attrSetName = 'my_custom_attribute';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
	->load($attrSetName, 'attribute_set_name')
	->getAttributeSetId();

//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
	->getCollection()
	->addAttributeToSelect('name')
	->addFieldToFilter('attribute_set_id', $attributeSetId);

//process your product collection as per your bussiness logic
$productsName = array();
foreach($products as $p){
	$productsName[] = $p->getData('name');
}
//return all products name with attribute set 'my_custom_attribute'
print_r($productsName); 

That’s it, have happy time with magento 🙂 !!!