Friday, March 29, 2013

Custom error/success message in Magento


>> A simple and most needed thing that we frequently use in Magento that how to add custom Error and Success message in Controller page.

You just need to call these methods:
$session = Mage::getSingleton('core/session');  
// You can specify the session with yours like:  
// Mage::getSingleton('customer/session'),
// Mage::getSingleton('checkout/session')

$session->addSuccess('your message here');
$session->addError('your message here');


Tuesday, March 26, 2013

Special character of Regular Expression

Regular Expression :

>> Meaning of Special character [](){}|?+-*^$\

    []    => Square brackets can be used to define a character group. [abc] The example finds one of the characters a, b or c.   

    -    => The hyphen can be used to define a range of characters. [a-f] The example finds one of the characters a, b, c, d, e or f. Also in this example, the regular expression only matches one character.

    ^    => With the meta character ^ at the beginning of a character group, the character group is negated. That means, the example will match any character but not an a. [^a]

           ^a If the meta character ^ is not included into a character group, it stands for the beginning of a string or a line. The example would match all lines or strings beginning with a.

    $    => a$ Like the meta character ^ stands for the beginning of a string or a line, the character $ stands for its end. The example would match all strings or lines ending with an a.
   
            ^abc$ Here the meta characters ^ and $ are used together. This example would match all strings or lines which are equal to "abc".
            Example 1: abc
            Example 2: abc abc

Friday, March 22, 2013

Prevent visitors to view the store without login in Magento


>> Magento use methods :  _redirect() AND _redirectUrl()
for redirect in Mage_Core_Controller_Varien_Action controller. If you see these methods, they are using this code to redirect $this->getResponse()->setRedirect('your_url');

Now you just need to call like :
                        Mage::app()->getFrontController()
                               ->getResponse()
                               ->setRedirect(Mage::getBaseUrl('your_url');


I used this code to prevent visitors to view my store without login.
With the following code in head.phtml :

<?php
    $CusSession=mage::getSingleton('customer/session');
    $frontController = Mage::app()->getFrontController();
  
    if(!$CusSession->isLoggedIn() && $frontController->getRequest()->getActionName() != 'login')
    {
       $frontController->getResponse()->setRedirect(Mage::getUrl("customer/account/login"));
    }
?>




Wednesday, February 20, 2013

How to add image in magento admin product Grid


>> With the help of this code you can able to add product image in admin Grid View :

    Step 1:
    First you need to copy the core block to local. Copy Grid.php from app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php to
    app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php
    Here you'll find a method named _prepareColumns(), add the below code within this method
  
        $this->addColumn('product_image', array(
              'header'    => Mage::helper('catalog')->__('Image'),
              'align'     =>'left',
              'index'     => 'entity_id',
              'width'     => '100px',
              'renderer'  => 'Mage_Adminhtml_Block_Catalog_Product_Renderer_Image'
        ));
  

    Step 2:
    Now create the renderer file named Image.php in the following path
    app/code/local/Mage/Adminhtml/Block/Catalog/Product/Renderer/Image.php
    Here add the below code..
  
    class Mage_Adminhtml_Block_Catalog_Product_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
        public function render(Varien_Object $row)
        {
            $_product = Mage::getModel('catalog/product')->load($row->getEntityId());
            if($_product->getImage() != 'no_selection'){
                  $image = "<img src='".Mage::helper('catalog/image')->init($_product, 'image')->resize(100)."' title='".$_product->getName()."' />";
            }
            return $image;
        }
    }
  
  
  
   

    

Tuesday, February 19, 2013

Use INR symbol in magento


 >> By default magento shows indian rupee as "Rs". If you want to show the symbol.


        &#8377; = ₹
        &#8360; = ₨

      i.e.  &#8377; 1,200 would show ₹ 1,200

     Note* : Use the font DejaVu Sans <span style='font-family: DejaVu Sans;'>&#8377;</span> for Internet Explorer and Safari.

>>  Go to Admin=>System=>Configuration=>Manage Currency=>Symbols. Replace the symbol.


>>  Here is a useful extension that will help you to replace rupee to new symbol. You can configure this one.

       http://www.magentocommerce.com/magento-connect/et-currency-manager.html


Tuesday, January 22, 2013

How to resize product/custom image in magento


>> Resize product image with product object.

          <img src="<?php echo Mage::helper('catalog/image')->init($_product, 'image')->resize(70,155);?>"/>


>> Resize a custom Image.

        // Image name
        $image = "";

        // actual image path
        $imageUrl = Mage::getBaseDir('media'). DS .'justonestepsolution'. DS .$image;
    
        // Give resized image path to be saved
        // here, the resized image will save in media/justonestepsolution/resized folder
        $imageResized = Mage::getBaseDir('media'). DS .'justonestepsolution'. DS .'resized'. DS .$image;
    
        // image will resize only if the image file exists and the resized image file doesn't exist

        if (!file_exists($imageResized) && file_exists($imageUrl))
        {
            $imageObj = new Varien_Image($imageUrl);
            $imageObj->constrainOnly(TRUE);
            $imageObj->keepAspectRatio(TRUE);
            $imageObj->keepFrame(FALSE);
            $imageObj->resize(300, null);
            $imageObj->save($imageResized);
        }


enjoy :-)



Tuesday, January 15, 2013

Get current logged customer details in magento


>>  $CusSession=mage::getSingleton('customer/session');
    //Checking is logged in customer
    if($CusSession->isLoggedIn())
    {
        $customer    = $CusSession->getCustomer();     // Customer object
        $name        = $customer->getName();
    }