2

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 ?

2 Answers 2

6
$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; } } 
5
  • I want to get all this data in backend, but if i not commented this line of code: $label = $attribute->getStoreLabel($product); it gives me in the firebug , 404. Am using to get all of this value an ajax . any ideea why it gives me the 404? the rest of it works fine, thx Commented Nov 12, 2014 at 9:18
  • you have to set store id for product might solve your issue. $store = Mage::app()->getDefaultStoreView(); $storeId = $store->getStoreId(); $product->setStoreId($storeId); Commented Nov 12, 2014 at 9:28
  • I put this code before the loop: $store = Mage::app()->getDefaultStoreView(); $storeId = $store->getStoreId(); $product->setStoreId($storeId); but still the same Commented Nov 12, 2014 at 9:33
  • try $product->setStoreId($storeId); after load product Commented Nov 12, 2014 at 9:37
  • please update your post with this Commented Nov 12, 2014 at 9:45
1
$prod=Mage::getModel('catalog/product')->load(8536); echo "<pre>"; print_r($prod->getData()); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.