Jun 25, 2014
kalpesh

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

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

Get state name from state id

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

Get state id from state code

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

Magento: Sample apache virtualhost for your website

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

1
2
3
4
5
6
7
8
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/var/www/magento/"
    ServerName loca.lho.st
    ServerAlias loca.lho.st
    ErrorLog "logs/error_log"
    CustomLog "logs/access_log" common
</VirtualHost>

Add entry to /etc/hosts too:

1
127.0.0.1    loca.lho.st

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

1
http://loca.lho.st

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

Jun 22, 2014
kalpesh

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!--
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * 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:
 * http://opensource.org/licenses/afl-3.0.php
 * 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 license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * 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 http://www.magentocommerce.com for more information.
 *
 * @category   Mage
 * @package    Mage_Core
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
-->
<config>
    <global>
        <install>
            <date><![CDATA[Mon, 23 Sep 2013 19:53:16 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[414c9022922d31b62bbe4447356e4ed6]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[127.0.0.1]]></host>
                    <username><![CDATA[MYSQL_USERNAME]]></username>
                    <password><![CDATA[MYSQL_PASSWORD]]></password>
                    <dbname><![CDATA[DATABASE_NAME]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
    <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>
Jun 22, 2014
kalpesh

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
echo "Start Cleaning all caches at ... " . date("Y-m-d H:i:s") . "\n\n";
ini_set("display_errors", 1);

require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);
Mage::getConfig()->init();

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

try {
    echo "Cleaning data cache... \n";
    flush();
    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... ";
    flush();
    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.

Jun 22, 2014
kalpesh

Magento: Enable logs on API calls

There is no setting to enable/disable API calls in Magento, but you can do so by modifying a file which handles all the API calls and logging the requests in a custom log file. This is useful when you know that there are many API calls being requested by third party applications to your website but not sure what is actually being called, how many times, and at what day and time.

With that said, open the file: app/code/core/Mage/Api/Model/Server/Handler/Abstract.php
and find the function “call” which handles all the API calls. Scroll down the function and find the if block which says:

1
2
3
4
5
6
7
8
9
10
11
if (method_exists($model, $method)) {
        if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
            return $model->$method((is_array($args) ? $args : array($args)));
        } elseif (!is_array($args)) {
            return $model->$method($args);
        } else {
            return call_user_func_array(array(&$model, $method), $args); 
        }       
} else {
        throw new Mage_Api_Exception('resource_path_not_callable');
}

and replace it with:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (method_exists($model, $method)) {
        Mage::log($method, null, 'api.log'); //logs the method
        Mage::log($args, null, 'api.log'); //logs the arguments
        if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
            return $model->$method((is_array($args) ? $args : array($args)));
        } elseif (!is_array($args)) {
            return $model->$method($args);
        } else {
            return call_user_func_array(array(&$model, $method), $args);
        }
} else {
        throw new Mage_Api_Exception('resource_path_not_callable');
}

Now you should have your var/log/api.log file filled with the “method” and “arguments” requested by third party systems on your server. This should help you in knowing what was requested and at what time in your new log file.

Pages:1234567...28»

Welcome to my Blog

This blog is merely to record the things that I find interesting. It can be technical, humor, news, creative or anything that deserves to be posted here. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Alexa Rank