Thursday, February 20, 2014

Magento Notes - II

Let's discuss some more Magento 1.8 stuffs ::

1. On your Cart page, how to get each product's category names?
    
   Put the following code where you want to show Category Name in  app\design\frontend\YOUR_PACKAGE\YOUR_THEME\template\checkout\cart\item\default.phtml   

<?php
   // GEt Product ID
   $proid = $this->getProduct()->getId();

   // GEt Category IDs
   $ids = $this->getProduct()->getCategoryIds();

   // Show Category Names
   $cat_name = "";

   foreach($ids as $val) 
   {
$cat_name .= Mage::getModel('catalog/category')->load($val)->getName() . ", ";
   }
   echo "Category : " . rtrim( $cat_name, ", ");
 ?>

2. How to get custom text attribute of a product?

   Suppose you have created a new text attribute called 'custom_desc'. Now, on various pages, you want to get this attribute's value. Insert any of the following codes to get it done.

   <?php
  // Method 1
  $custom_desc =  $_product->getData("custom_desc"); 
   
  // Method 2
  $custom_desc = $_helper->productAttribute($_product, nl2br($_product->getCustomDesc()), 'custom_desc'); 
   
  // MEthod 3 :: Easiest
  $custom_desc = $_product->getCustomDesc(); 
 ?>
   
   Amazing is the fact that if we create a property/attribute called "test", calling "$_product->getTest()" would return value of that property for the product "$_product". Such custom named functions will be automatically available with the product object. Check more examples below.
   
   "short_description"      => $_product->getShortDescription()
   "country_of_manufacture" => $_product->getCountryOfManufacture()
   
3. How to get Customer’s Email Address into the Order Notification Email Templates ?
  
  You need to insert the following shortcode into various email template files stored in app\locale\en_US\template\email\sales\ folder.
  
  {{htmlescape var=$order.getCustomerEmail()}}
  
4. How to identify whether current page is the home page?
   
   Use the following code snippet to help ...

   <?php
   
  $is_homepage = Mage::getBlockSingleton('page/html_header')->getIsHomePage();  
   if( $is_homepage )
   {
      // Do, if Home Page
      // However this won't work if you are sitting
      // on Home Page, but you have extra slashes in
      // URL like abcd.com/?p=2 or abcd.com/home?page=2
   }
 ?>

Another most trustworthy solution is ::

   <?php
  if( Mage::getSingleton('cms/page')->getIdentifier() == 'home' )   {
      // Do, If Home Page
      // This would work most of the times as usually 'home'
      // identifier (for page) is what we always set as Home
  }
 ?>  

5. What to do when Products are not showing up in Category page or Search results?

    1. Check if product is enabled
    2. Check product Quantity is valid. However if "Display out of stock products" is set to "yes", then products will be shown even if it has zero quantity.
    3. Check if product is assigned to a category
    4. Check Attribute settings, check if product attributes like "name" etc are "Searchable"
    5. Check if "name" attribute is having a "Global" scope
    6. Clear Cache
    7. Re-Index all the indexes

6. How to get Stock Quantity of the current product?
    
Use the following code to get current product's stock quantity. You may replace the product object "$_product" with other object you are working with.

   <?php
    $_quantity = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty());
 ?>

Check out Magento Notes - I

No comments: