Product – Get All Attribute Names Using Product ID or SKU

productproduct-attribute

I want to get all of the attributes for a specific product using its id or sku. This is my try:

$prod=Mage::getSingleton('catalog/product')->load(8536);
var_dump($prod->getAttributeText());

but i got this error message: Call to a member function getSource() on a non-object in… What should I do ?

Best Answer

$store = Mage::app()->getDefaultStoreView(); 
$storeId = $store->getStoreId();

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


// get all attributes of a product
foreach ($attributes as $attribute) {      
    $attributeCode = $attribute->getAttributeCode();
    $label = $attribute->getStoreLabel($product);   
    $value = $attribute->getFrontend()->getValue($product);
    echo $attributeCode . '-' . $label . '-' . $value; echo "<br />";    
}


//get only frontend attributes of a product
foreach ($attributes as $attribute) {
    if ($attribute->getIsVisibleOnFront()) {
        $attributeCode = $attribute->getAttributeCode();
        $label = $attribute->getFrontend()->getLabel($product);      
        $value = $attribute->getFrontend()->getValue($product);
        echo $attributeCode . '-' . $label . '-' . $value; echo "<br />";    
    }
}

// get name and value of particular attribute of a product
foreach ($attributes as $attribute) {   
    $attributeCode = $attribute->getAttributeCode();
    $code = 'color';
    if ($attributeCode == $code) {
        $label = $attribute->getStoreLabel($product);   
        $value = $attribute->getFrontend()->getValue($product);
        echo $attributeCode . '-' . $label . '-' . $value;
    }
}
Related Topic