Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5488

Magento Product Attributes

$
0
0

Get Custom Attributes Value in Frontend.

1. Suppose our custom attribute code is “test_attr” and of type Text Field or Text Area .

<?php
     $product = Mage::getModel("catalog/product")->load($product_id);
     echo $product->getTestAttr();
?>

I use getTestAttr() because my attribute code is “test_attr”, Use uppercase after underscore or begining of character, like

for “testattr” method should be getTestattr()

for “test_attr” method should be getTestAttr()

2. Suppose our custom attribute code is “test_attr” and of type Multiselect or Dropdown .

<?php
     $product = Mage::getModel("catalog/product")->load($product_id)
     $attributeCode = 'test_attr';
     $AllAttributes = $product->getAttributes();
        if(array_key_exists($attributeCode , $AllAttributes)){
            $Attribute = $AllAttributes["$attributeCode"];
            $Value = $Attribute->getFrontend()->getValue($product);
        }
     echo $Value;
?>

3. To get all the options of attributes .

<?php  
     $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','test_attr');
     $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
     $attributeOptions = $attribute->getSource()->getAllOptions();
     foreach($attributeOptions as $each){
       echo $each["label"]." ".$each["value"]."<br>";
     }
?>

Enjoy !!

Facebook Twitter Email Reddit Stumbleupon

Viewing all articles
Browse latest Browse all 5488

Trending Articles