Wednesday, 3 February 2016

How to get custom attribute value in Magento

Display custom attribute value in Magento


When we get custom attribute value in Magento through simple way like:



<?php 


echo $_product->getAttributeText('custom_attribute_code');


?>


Then we will get a fatal error (as following) , if the custom attribute is not already added. 


" Fatal error: Call to a member function getSource() on a non-object in ...\app\code\core\Mage\Catalog\Model\Product.php on line 1388"


So we will use following code for get custom attribute value in Magento.

The following code will first check that the custom attribute are added or not in admin. If custom attribute are added then get its value.




<?php 

$attribute = $_product->getResource()->getAttribute('custom_attribute_code');

         if ($attribute)

          {

               echo $attribute_value = $attribute ->getFrontend()->getValue($_product);

          }

?>




Saturday, 30 January 2016

How to get Review/Rating summary in Magento

We can use the below code to retrieve product Review/Rating summary in Magento



<?php 


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


$storeId    = Mage::app()->getStore()->getId();


$summaryReview = Mage::getModel('review/review_summary')
  ->setStoreId($storeId)
  ->load('product_id');


  if($summaryReview->getRatingSummary()){


  ?>


  <div class="rating-box" style="float:left;">


<div class="rating" style="width: <?php echo $summaryData->getRatingSummary().'%'; ?>">

                             </div>
  </div>
<?php 
}
?>

How to display category name by product id in Magento

If you want to get Category Name by Product Id in Magento. Use following code:



<?php 

$product = Mage::getModel('catalog/product')->load($_product->getId());


$cat_id = $product->getCategoryIds();


foreach($cat_id as $categorycbId) {


$category_id = Mage::getModel('catalog/category')->load($categorycbId);


echo $category_id->getName();


}

?>


Friday, 29 January 2016

How to display static block with title in phtml file : Magento

If you want to display static block in PHTML file in Magento. We can use following code:


<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('identifier')->toHtml() ?>



But If you want to display static block with title. So, we can you following code:


<?php
     $block = Mage::getModel('cms/block')--->load('identifier');
     echo $block->getTitle();
     echo $block->getContent();
?>