Browsing articles in "Magento"
Aug 21, 2014

Magento check if customer registered in checkout page

If you want to check if the customer is guest, registered or just register to the site when they place the order, below script will help you identify that in success.phtml file.

You can find success / order confirmation phtml file at:

Just at the end of this file put below lines of code:

<?php $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$quoteId = $order->getQuoteId();
$quote = Mage::getModel('sales/quote')->load($quoteId);
$method = $quote->getCheckoutMethod(true);
$customer_email = $order->getCustomerEmail();
if ($method == 'register'){ ?>
//code to handle if customer just registered to your site
<?php } elseif($method == 'guest') {?>
//code to handle if customer is guest
<?php } else { ?>
//code to handle for logged in customer
<?php } ?>

In the same file, success.phtml, you can request for order number, customer email, customer id, subtotal, grandtotal, order ID just created etc. like this:

$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$_totalData =$order->getData();
$_sub = $_totalData['subtotal'];
$_orderEmail = $_totalData['customer_email'];
$_orderNumber = $_totalData['increment_id'];
Jun 25, 2014

Magento get State Code, ID, Name from Region ID

Magento get state name, ID, code from region ID or region code.

Get state code from state id

$region = Mage::getModel('directory/region')->load(12);
$state_code = $region->getCode(); //CA

Get state name from state id

$region = Mage::getModel('directory/region')->load(12);
$state_name = $region->getName(); //California

Get state id from state code

$region = Mage::getModel('directory/region')->loadByCode('CA', 'US');
$state_id = $region->getId(); //12
Jun 22, 2014

Magento: Sample apache virtualhost for your website

Sample apache virtualhost to point to your magento directory and run your local website with specified URL.

<VirtualHost *:80>
    DocumentRoot "/var/www/magento/"
    ErrorLog "logs/error_log"
    CustomLog "logs/access_log" common

Add entry to /etc/hosts too:

Restart apache (service httpd restart OR service apache2 restart) and point your browser location to:

and you will see the website running from your /var/www/magento directory.

Jun 22, 2014

Magento: Sample local.xml template

Sample app/etc/local.xml file template. Change mysql database name, username and password with yours and clear the cache.

 * Magento
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to so we can send you a copy immediately.
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to for more information.
 * @category   Mage
 * @package    Mage_Core
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (
 * @license  Academic Free License (AFL 3.0)
            <date><![CDATA[Mon, 23 Sep 2013 19:53:16 +0000]]></date>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
Jun 22, 2014

Magento: Clear all caches from command line

Magento clear all caches from command line, programatically from ssh. Clearing the caches is a must when you are making any configuration changes in your Magento website. Although you can always clear the cache from admin panel, sometimes for faster cleaning or unable to log into admin panel reason, it’s good to have a script which will clear all the caches in Magento.

Create a file in your Magento root and name it clearCache.php with the below code:

echo "Start Cleaning all caches at ... " . date("Y-m-d H:i:s") . "\n\n";
ini_set("display_errors", 1);

require 'app/Mage.php';

$types = Mage::app()->getCacheInstance()->getTypes();

try {
    echo "Cleaning data cache... \n";
    foreach ($types as $type => $data) {
        echo "Removing $type ... ";
        echo Mage::app()->getCacheInstance()->clean($data["tags"]) ? "Cache cleared!" : "There is some error!";
        echo "\n";
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");

echo "\n";

try {
    echo "Cleaning stored cache... ";
    echo Mage::app()->getCacheInstance()->clean() ? "Cache cleared!" : "There is some error!";
    echo "\n\n";
} catch (exception $e) {
    die("[ERROR:" . $e->getMessage() . "]");

Make sure all the double quotes comes good in copy pasting.

You can now run this script by the command “php -f clearCache.php” from your magento root location in terminal and this will start clearing all the caches for you! Once done, it will confirm by the message “Cache cleared!” or giving error message if it fails.

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!




Magento top 50 contributors

Magento top 50 contributors