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"));
    }
?>