Search This Blog

Tuesday, 20 November 2012

Working with date functions in Magento


The date time will be calculated based on the lacale timezone settings in administration:

    $anyDate = '2012-11-21 13:00:00';
    $dateTimestamp = Mage::getModel('core/date')->timestamp(strtotime($anyDate));
    echo $currentDate = date('Y-m-d H:i:s', $dateTimestamp);

Click here for more details




Other PHP scripts to find the offset for the timezone

/** Script to calculate the offset seconds fro the timezone */
$timezone = new DateTimeZone("Europe/London");
$offset = $timezone->getOffset(new DateTime("now")); // Offset in seconds
echo ($offset < 0 ? '-' : '+').round($offset/3600).'00'; // prints "+1100"

/** Script to convert the date and time to specific timezone */
$value = "2012-09-21 13:00:00";
$date = new DateTime($value);
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s');

Monday, 19 November 2012

Magento: Get current Module Name, Controller Name and Action Name

$req = Mage::app()->getRequest();
$req->getModuleName();
$req->getControllerName();
$req->getActionName();

Thursday, 8 November 2012

Magento: Create new payment method

Magento: Display Currency Switcher in Header

Add the following XML in layout/directory.xml between <default> and </default>

        <reference name="header">
            <block type="directory/currency" name="top.currency"  template="directory/currency.phtml"/>
        </reference>

Add following code in header.phtml

<?php echo $this->getChildHtml('top.currency') ?>

Magento: Change the store currency

$store = Mage::app()->getStore();

$store->setCurrentCurrencyCode('GBP');

Note: 
Make sure the currency is enabled for the specific store
Make sure the currency conversion rate has been populated and saved

Tuesday, 6 November 2012

Magento: Events and Observers

One of the greatest feature in Magento is Events and Observers.

You can add any functionality for the specific Event without following:
1. Modifying core files
2. Copy core file to local folder
3. Override Core files
4. Extend core methods

Here are the links for best examples:
  1. http://codemagento.com/2011/04/observers-and-dispatching-events/
  2. http://magedev.com/2009/08/11/magento-events-create-update/
  3. http://magedev.com/2010/10/15/adding-event-observer-on-the-fly/
You can create custom events in your Own module as well

Using events is the best way of extending Magento functionality......