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 ?

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;
    }
}
  • 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 – Attila Naghi Nov 12 '14 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); – liyakat Nov 12 '14 at 9:28
  • I put this code before the loop: $store = Mage::app()->getDefaultStoreView(); $storeId = $store->getStoreId(); $product->setStoreId($storeId); but still the same – Attila Naghi Nov 12 '14 at 9:33
  • try $product->setStoreId($storeId); after load product – liyakat Nov 12 '14 at 9:37
  • please update your post with this – Attila Naghi Nov 12 '14 at 9:45
1
$prod=Mage::getModel('catalog/product')->load(8536);
echo "<pre>"; print_r($prod->getData());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.