Wednesday, May 30, 2012

URL redirection after tracking the visitor's IP address it will automatically land the visitor to the corresponding website in Magento

First crate a file in root folder named: ip.php, and copy/paste following code or download from http://ipinfodb.com/index.php


<?php
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = 'efd67dca864cbe8b45fb21b7d19916bf471afd1bfcfe0b3e975d1f238d6c7ed3';

public function __construct(){}

public function __destruct(){}

public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}

public function getError(){
return implode("\n", $this->errors);
}

public function getCountry($host){
return $this->getResult($host, 'ip-country');
}

public function getCity($host){
return $this->getResult($host, 'ip-city');
}

private function getResult($host, $name){
$ip = @gethostbyname($host);

if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');

try{
$response = @new SimpleXMLElement($xml);

foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}

return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}

$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>


Then open index file of magento nad copy/paste following code at the top


$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$url = $protocol.'://'.$_SERVER['SERVER_NAME']; //  More efficient manner
//echo $url;

include('ip.php');
$ipLite = new ip2location_lite;
//$ipAddress = '62.68.127.255';
$ipAddress = $_SERVER['REMOTE_ADDR'];
$locations = $ipLite->getCountry($ipAddress);
$countryCode = strtoupper(trim($locations['countryCode']));
//$countryCode='CA';
//$country_cd = substr($_SERVER['SERVER_NAME'],0,-2);



switch($countryCode){
case 'CA': /* Cananda */
if($url != 'http://www.yourdomain.ca'){
header('location: http://www. yourdomain .ca'.$_SERVER['REQUEST_URI']);
}
break;

case 'UK': /* United States */
if($url != 'http://www. yourdomain .co.uk'){
header('location: http://www. yourdomain .co.uk'.$_SERVER['REQUEST_URI']);
}
break;

case 'DE': /* Germany/Dutch */
if($url != 'http://www. yourdomain .de'){
header('location: http://www. yourdomain .de'.$_SERVER['REQUEST_URI']);
}
break;

case 'SE': /* Sweden */
if($url != 'http://www. yourdomain .se'){
header('location: http://www. yourdomain .se'.$_SERVER['REQUEST_URI']);
}
break;

/* For European Countries Start */

case 'RU': //Russia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'FR': //France
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'UA': //Ukraine
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'ES': //spain
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'SE': //sweden
if($url != 'http://www.handbagbutler.eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'NO': //Norway
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'FI': //Finland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'PL': //Poland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'IT': //Italy
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'RO': //Romania
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'BY': //Belarus
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'KZ': //Kazakhstan
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'GR': //Greece
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'BG': //Bulgaria
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'IS': //Iceland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'HU': //Hungary
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'PT': //Portugal
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'RS': //Serbia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu/'.$_SERVER['REQUEST_URI']);
}
break;

case 'AT': //Austria
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'CZ': //Czech Republic
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'IE': //Republic of Ireland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'GE': //Georgia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'LT': //Lithuania
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'LV': //Latvia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'RO': //Croatia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'BA': //Bosnia and Herzegovina
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

/* For European Countries End */

default: /* Default */
if($url != 'http://www. yourdomain .com'){
header('location: http://www. yourdomain .com'.$_SERVER['REQUEST_URI']);
}
break;
}




Tuesday, May 29, 2012

How to get full url of website uing php

$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$url = $protocol.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; //  More efficient manner
echo $url;

Monday, May 28, 2012

How to send Email in magento

Use following code
<?php
$mail = Mage::getModel('core/email');
$mail->setToName('Your Name');
$mail->setToEmail('Youe Email');
$mail->setBody('Mail Text / Mail Content');
$mail->setSubject('Mail Subject');
$mail->setFromEmail('Sender Mail Id');
$mail->setFromName("Msg to Show on Subject");
$mail->setType('html');// YOu can use Html or text as Mail format

try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
$this->_redirect('');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
$this->_redirect('');
}
?>


To send mail with attached file, prefer use of Zend_Mail

try{
            $mail = new Zend_Mail();
            $mail->setFrom("fromemail","fromname");
            $mail->addTo("toemail","toname");
            $mail->setSubject("subject");
            $mail->setBodyHtml(" body text"); // here u also use setBodyText options.

            // this is for to set the file format
            $at = new Zend_Mime_Part($content);

            $at->type        = 'application/csv'; // if u have PDF then it would like -> 'application/pdf'
            $at->disposition = Zend_Mime::DISPOSITION_INLINE;
            $at->encoding    = Zend_Mime::ENCODING_8BIT;
            $at->filename    = $filename;
            $mail->addAttachment($at);
            $mail->send();

        }catch(Exception $e)
        {
            echo $e->getMassage();

        }

Saturday, May 26, 2012

How to remove index.php from magento URL

There are the following steps

step 1: Goto to your site root folder and you can find the htaccess file there.Open it on text editor and find the line #Rewrite Base/ magento. Just replace it with Rewrite Base/

step 2: Then goto your admin panel and enable the Rewrites. You can find it at System->Configuration->Web->Search Engine Optimization

step 3: Then goto Cache management page (system cache management ) and refresh your cache and refresh the Web Rewrites.

Sunday, May 20, 2012

How to set up a Magento store for Australia


Running Magento stores in Australia differs very little from any other country, but there are a few things that you need to do for compliance with ATO requirements and to ensure that Magento calculates taxes correctly. The following is an outline of the necessary configuration and settings you may need to consider when setting up Magento for an Australian store.

First, the basic information for the store must be configured for Australia:
System → Configuration → General
Countries options

    * Default country = Australia
    * Allow countries = ensure Australia is selected

Locale options

    * Timezone = AUS Eastern Standard Time (Australia/Sydney) (as appropriate)
    * Locale = English (Australia)

Next, the currency must be configured for Australian Dollars:
System → Configuration → Currency Setup
Currency Options

    * Base currency = Australian Dollar
    * Default display currency = Australian Dollar
    * Allowed currencies = ensure Australian Dollar is selected

Shipping settings are important for shipping, and also for tax calculation. You should enter information appropriate for your shipping location, which must be in Australia for shipping and tax calculations to work correctly.
System → Configuration → Shipping Settings
Origin

    * Country = Australia

To set tax correctly for Australia (prices shown include GST) and to apply discounts correctly, the following should be set:
System → Configuration → Tax
Tax Classes

    * Tax Class for Shipping = Shipping

Calculation Settings

    * Tax Based On = Shipping Address
    * Catalog prices include tax = Yes
    * Shipping prices include tax = Yes
    * Apply Tax After Discount = Yes
    * Apply Discount On Prices Including Tax = Yes
    * Apply Tax On = Custom price if available

Default Tax Destination Calculation

    * Default Country = Australia

Display

    * Display Cart/Order Prices = Including tax
    * Display full tax summary = No
    * Display Shipping Prices = Including tax
    * Display Product Prices = Including tax

These settings will instruct Magento that all prices entered into the system already include GST (which is most common for Australian retailers) and that prices displayed to the customer should be displayed including tax. You may need to adjust the settings for shipping appropriately, depending on whether your chosen shipping method should include GST or not.

Please note that the upcoming release has some changes to these settings. For Community Edition 1.4 (and Enterprise Edition 1.6), use the following instead:
System → Configuration → Tax
Tax Classes

    * Tax Class for Shipping = Shipping

Calculation Settings

    * Tax Calculation Method Based On = Total
    * Tax Calculation Based On = Shipping Address
    * Catalog prices = Including tax
    * Shipping prices = Including tax
    * Apply Customer Tax = After Discount
    * Apply Discount On Prices = Including tax
    * Apply Tax On = Custom price if available

Default Tax Destination Calculation

    * Default Country = Australia

Price Display Settings

    * Display Product Prices In Catalog = Including tax

Shopping Cart Display Settings

    * Display Prices = Including tax
    * Display Subtotal = Including tax
    * Display Shipping Amount = Including tax
    * Include Tax in Grand Total = No

Order, Invoices, Creditmemos Display Settings

    * Display Prices = Including tax
    * Display Subtotal = Including tax
    * Display Shipping Amount = Including tax
    * Include Tax in Grand Total = No

The easiest way to meet the ATO requirements for ABN and contact number on invoices is to add these details to your address information:
System → Configuration → Sales
Invoice and Packing Slip Design

    * Address = your address + ABN and phone number

You might also choose to add this information to the transactional email template for invoices. If you want to use the PDF invoices however you will need to alter the address above or modify the PDF rendering, which we may cover in another post.

To set up the 10% GST to be applied on products and shipping, Magento requires the addition of an appropriate tax rate and rule:
Sales → Tax → Manage Tax Zones & Rates

Here we add a tax rate for GST so that Magento will know how much tax to calculate.

To add a rate, click Add New Tax Rate in the top right and fill out the following information:

    * Tax Identifier = GST
    * Country = Australia
    * State = *
    * Zip/Post Code = *
    * Rate = 10.00

Save the rate and proceed to set up a rule for GST.
Sales → Tax → Manage Tax Rules

Tax rules determines when rates should be applied. In this case we want to set it up to apply GST anywhere in Australia.

Click Add New Tax Rule in the top right and fill out the following information:

    * Name = GST
    * Customer Tax Class = Retail Customer
    * Product Tax Class = Taxable Goods | Shipping
    * Tax Rate = GST

Save the rule and GST will be completely set up. It's a good idea to remove any other tax rules that are present by default, unless they will also be relevant for you.
Rename Tax to GST

Something that is often overlooked is that you must include a line in your invoices specifically indicating the amount of GST that has been applied. This can be done in a few ways but a simple method is to use the translation files included as part of the locale. This allows for easy replication across websites by copying the files from one site to another.

To modify the translation, creating an app/locale/en_AU directory and copy the Mage_Sales.csv file from the en_US locale into it. Open this file as a spreadsheet (such as in Excel or OpenOffice.org) and find the line with the word "Tax". Modify the value in the right column to be "GST". This will appropriately change the text that is displayed to the customer.
Rename Invoice to Tax Invoice

Similar to the above, you can edit the translation files to change the text "Invoice" to "Tax Invoice". This is left as an exercise for the reader.

After applying all of these changes you'll have addressed the essentials for running a Magento store in Australia. We recommend also installing our Magento Australian extension, which adds Australian regions, Australia Post shipping, direct deposit and BPAY payment methods. Also be sure to watch out for Magento Community Edition 1.4 as it includes several key improvements to the tax system to make working with discounts easier.

Thursday, May 3, 2012

Design and Template


The first step in any magento customization is design changes (both CSS and HTML Changes). Magento comes with a default theme, but usually people require some changes in their theme depending on their websites. Minor text add/edits, layout changes and other changes are usually required.

Magento template system is very powerfully, it gives you the ability to change almost anything. The template system consists of layout xml files, css files and phtml files. In a typical PHP application, we have HTML/PHP and CSS files where we create/edit the design/layout of the website.  But in magento we have .phtml files to do the job html files. Magento follows MVC architecture and .phtml file do the job of VIEW. phtml are meant only for creating the structure of a page through html/php. They can be used to write complex php codes as well, but generally magento doesn’t follow this standard.

The default folders in magento which contain css and phtml files are:

CSS: skin\frontend\default\default\
PHTML:  app\design\frontend\base\default\template\
Layout: app\design\frontend\base\default\layout\
Before going into magento development we need to remember few things (general good practices to follow, not compulsory):

1. We should never to change magento core files. The reason being, whenever magento is updated all core files are overwritten. So if you make changes in core files those changes will be lost. Core files means, files that come with default magento package. All php, xml and .phtml files where magento code is written.

2. Always remember to turn of magento cache before starting development work. If cache is Turned On, your latest change might not show up in magento. To turn off cache you need to go to magento admin. From top menu go to System -> Cache Management.  Then select all cache types and disable them.


Now let’s start with a very simple task: We will simple remove the default magento logo from the header.

To do this, first we need to know which file to make the changes. For this, magento has a system called Template Path Hints. After enabling temple path hints, you need to refresh your magento home page, you see red color boxes, with paths of phtml files in them. Basically, magento tell you from which file has the current content on the page is coming from. So as you can see from the hints, that header comes from the phtml frontend/base/default/template/page/html/header.phtml , we can simply edit this file and remove the logo.
But, since as a general we should not edit any magento core files, we will create our new theme. In this theme we will do our edits and tell magento to use our theme instead of default theme.

To create a new theme in magento, open magento admin and go to System->Configuration->General->Design->Themes

Follow steps below to create a new theme

1. Let say our new theme name is “newtheme”.

2. Next need to first create folders for our theme. For the PHTML files, create folder named “newtheme” in app/design/frontend/default/ and copy the contents of app/design/frontend/base/default into it.

3. And for CSS files, create a folder name “newtheme” in skin/frontend/default/newtheme and copy the content of skin/frontend/default/default into.

To test your new theme, enable template path hints again and see if files from your theme are shown there or not. You can now make changes to your theme instead of default theme.
So to remove the magento logo, go to the header.phtml file in your new theme and remove the HTML for the logo.