Thursday 14 June 2012

Remove Index.php in magento

If you want to access your magento URL without index.php
for example:
http://domain.com/index.php/category
to
http://domain.com/category
then use the following steps

1) Login to admin section by using the URL
http://domain.com/index.php/admin

2) then go to “System >>  Configuration >>Web >> Search Engines Optimization”
Use Web Server Rewrites : YES

3) Go to “System >>  Configuration >>Web >>Secure”
Use secure URL Frontend: YES

4)Then create the .htaccess file under your the magento installed folder.
If the magento installed under document root ( /home/username/public_html) then add follogig rules into .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

and If the magento installed under /shop or directory then add the following rules into ” /home/username/public_html/shop/.htaccess ” file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /shop/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shop/index.php [L]
</IfModule>

sample coding for google checkout


<html>

<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"   >
        </script>
<script type="text/javascript">
   

    /*
    Requires: PrototypeJS
    http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js
    */

    document.observe("dom:loaded", function() {
        $("theGoogleForm").onsubmit = function() {
            var itms = [];
            //get checkboxes with name=selections
            //use a different name for other checkbox groups in the theGoogleForm (if any)
            //You can extend this to handle multiple checkbox groups by omitting the name parameter for later evaluation.
            var checks = this.getInputs("checkbox", "selections");
            checks.each(function(c) {
                if (c.checked) {
                    var itm = $F(c).split("|");
                    itms.push({ "item_name": itm[0], "item_description": itm[1], "item_price": itm[2], "item_weight": itm[3] });
                }
            });
            //Change 2 to any number of "required checked items" you need.
            //Change validation as necessary, currently, user must select exactly 2 items
            if (itms.length == 2) {
                var x = 1;
                var tgf = $("theGoogleForm");
                //build the elements based on selections
                itms.each(function(i) {
                    tgf.appendChild(e("item_name_" + x, i.item_name));
                    tgf.appendChild(e("item_description_" + x, i.item_description));
                    tgf.appendChild(e("item_price_" + x, i.item_price));
                    tgf.appendChild(e("item_weight_" + x, i.item_weight));
                    tgf.appendChild(e("item_weight_unit_" + x, "LB"));
                    tgf.appendChild(e("item_currency_" + x, "USD"));
                    tgf.appendChild(e("item_quantity_" + x, "1"));
                    x++;
                });
                //evaluation test was met, send to Google Checkout
                return true;
            } else {
                //evaluation test was not met, prevent form submission
                alert("A selection of 2 coffee blends is needed.\nYou selected " + itms.length + " blends.");
                return false;
            }
        };
    }
    );

    function e() {
        return new Element("input", { name: arguments[0], value: arguments[1], type: "hidden" });
    };
</script>

<!-- Replace with your sandbox MID. Replace url and mid for production. -->

<form action="https://sandbox.google.com/checkout/api/checkout/v2/checkoutForm/Merchant/302986852433793"
            id="theGoogleForm" method="post" name="BB_BuyButtonForm">
            <p>
                PayCircuit Peet's Coffee Sale: Please select <b>any 2 blends</b> from this list</p>
            <ol>
                <li>
               
                    <!--
                    The value of each checkbox correspond to: item_name, item_description, item_price, item_weight fields.
                    Modify values as necessary, but do not change sequence (if you do you have to modify Javascript)
                    Values are delimited by pipe character |
                    //-->
                   
                    <input name="selections" type="checkbox" value="PayCircuit Coffees|Major Dickason|8.99|1" />Major
                    Dickason</li>
                <li>
                    <input name="selections" type="checkbox" value="PayCircuit Coffees|House Blend|7.99|1" />House
                    Blend</li>
                <li>
                    <input name="selections" type="checkbox" value="PayCircuit Coffees|Seasonal Blend|9.99|1" />Seasonal
                    Blend</li>
                <li>
                    <input name="selections" type="checkbox" value="PayCircuit Coffees|Decaf House Blend|8.99|1" />Decaf
                    House Blend</li>
                <li>
                    <input name="selections" type="checkbox" value="PayCircuit Coffees|Decaf Major Dickason|8.99|1" />Decaf
                    Major Dickason</li>
                <li>
                    <input name="selections" type="checkbox" value="PayCircuit Coffees|Decaf Seasonal Blend|10.99|1" />Decaf
                    Seasonal Blend</li>
            </ol>
           
            <!-- No item fields are in static HTML. -->
            <!-- Item fields are dynamically created by Javascript based on checkbox choices -->
            <!-- Shipping fields below can also be removed if you prefer to use your profile settings -->
           
            <input name="_charset_" type="hidden" value="utf-8" />
            <input type="hidden" name="ship_method_name_1" value="USPS Priority Mail" />
            <input type="hidden" name="ship_method_price_1" value="4.95" />
            <input type="hidden" name="ship_method_currency_1" value="USD" />
            <input alt="" src="https://sandbox.google.com/checkout/buttons/buy.gif?merchant_id=302986852433793&w=117&h=48&style=white&variant=text&loc=en_US"
                type="image" />
</form>


</body>
</html>

Test card with name

Card
Type
Card
Number
Number
Characters



Master
Card
5105105105105100                                (16)
Characters
Master
Card
5555555555554444 (16)
Characters
Master
Card
4222222222222 (13)
Characters
VISA 4111111111111111 (16)
Characters
VISA 4012888888881881 (16)
Characters



American
Express
378282246310005 (15)
Characters
American
Express
371449635398431 (15)
Characters
Amex
Corporate
378734493671000 (15)
Characters
Dinners
Club
38520000023237 (14)
Characters
Dinners
Club
30569309025904 (14)
Characters



Discover 6011111111111117 (16)
Characters
Discover 6011000990139424 (16)
Characters
JCB 3530111333300000 (16)
Characters
JCB                         3566002020360505                         (16)
Characters


                                                                        




Test Credit Card
Numbers:
Visa: 4111-1111-1111-1111
MasterCard: 5431-1111-1111-1111
Amex: 341-1111-1111-1111
Discover: 6011-6011-6011-6611




Credit Card Prefix Numbers (security code) :
Visa:
13 or 16 numbers starting with 4
MasterCard:
16 numbers starting with 5
Discover:
16 numbers starting with 6011
AMEX:





Test Number Test Name Credit Card Numbers Additional Instructions
1 AVS Full Match AMEX: 342955624318368
Discover: 6011624665174125
MasterCard: 5120939736834980
Visa: 4116509050569147
Enter AVS+FULL as the value of the Name field and the first line of the Address field.
2 AVS Partial Match - Postal Code only AMEX: 344058488426266
Discover: 6011976857117225
MasterCard: 5123695007103193
Visa: 4116480559370132
Enter AVS+POSTAL as the value of the Name field and the first line of the Address field.
3 AVS Partial Match - Address only AMEX: 345672983453416
Discover: 6011750048255222
MasterCard: 5122415444933925
Visa: 4119692004990226
Enter AVS+ADDR as the value of the Name field and the first line of the Address field.
4 AVS No Match AMEX: 342135898797783
Discover: 6011587918359498
MasterCard: 5106447623213738
Visa: 4112296825756779
Enter AVS+NOMATCH as the value of the Name field and the first line of the Address field.
5 AVS Unsupported by Issuer AMEX: 347279493269015
Discover: 6011604410824034
MasterCard: 5152370491539153
Visa: 4112836497303994
Enter AVS+UNSUPPORTED as the value of the Name field and the first line of the Address field.
6 Credit Card Verification Number Match AMEX: 345796298727014
Discover: 6011648040903965
MasterCard: 5155659853141666
Visa: 4110716719147654
Enter CVV+MATCH as the value of the Name field and the first line of the Address field.
Enter any four-digit CVC code other than 0000, 5555 and 9999 for American Express. Enter any three-digit CVC code other than 000, 555 and 999 for Discover, MasterCard or Visa.
7 Credit Card Verification Number Unavailable AMEX: 342650299263839
Discover: 6011118186183608
MasterCard: 5199339067794810
Visa: 4112530021797363
Enter CVV+UNAVAILABLE as the value of the Name field and the first line of the Address field.
Enter 0000 as the four-digit CVC code for American Express. Enter 000 as the three-digit CVC code for Discover, MasterCard or Visa.
8 Credit Card Verification Number Error AMEX: 342781835011463
Discover: 6011789277338945
MasterCard: 5102881917494147
Visa: 4112653620165475
Enter CVV+ERROR as the value of the Name field and the first line of the Address field.
Enter 9999 as the four-digit CVC code for American Express. Enter 999 as the three-digit CVC code for Discover, MasterCard or Visa.
9 Credit Card Verification No Match AMEX: 342337649030528
Discover: 6011769851691760
MasterCard: 5155239927190195
Visa: 4113864427850982
Enter CVV+NOMATCH as the value of the Name field and the first line of the Address field.
Enter 5555 as the four-digit CVC code for American Express. Enter 555 as the three-digit CVC code for Discover, MasterCard or Visa.
10 Credit Card Authorization Successful AMEX: 348771682068975
Discover: 6011739196887563
MasterCard: 5184778657904478
Visa: 4119862760338320
Enter AUTH+OK as the value of the Name field and the first line of the Address field.
11 Credit Card Authorization Declined See special instructions below. Note: Please see the special instructions below for testing a failed credit card authorization.




Google checkout HTML Api

Google Checkout's HTML API (including BUY NOW Buttons) are easily extended with server-side code. This usually involves creating a shopping cart using server side script, and dynamically creating HTML that conforms to Google's HTML API.
But what if you don't have access to server-side resources? This working sample is one way of creating "dynamic fields" with Javascript that conform to Google's HTML API. This is not meant to be replacement for server-side code, and, as with all client-side code, will not work on browsers with Javascript disabled.
What this sandbox sample does:
  • Will use checkboxes to specify items from a list for checkout
  • An additional requirement of 2 (and only 2, min/max) books must be selected from the list. You can modify this rule to anything you prefer.
  • Allows you to do (a little) more than just static HTML with BUY Now or HTML API. This sample turns a "simple" BUY NOW button, into a not-so-simple implementation of Google Checkout!
Note: There is an issue/bug with this sample when the user navigates back from Google Checkout without clicking "Edit Cart" (i.e. browser back arrow).
PayCircuit Peet's Coffee Sale: Please select any 2 blends from this list
  1. Major Dickason
  2. House Blend
  3. Seasonal Blend
  4. Decaf House Blend
  5. Decaf Major Dickason
  6. Decaf Seasonal Blend

Source/Reference

JavaScript

<script type="text/javascript">
    /*
    Requires: PrototypeJS
    http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js
    */

    document.observe("dom:loaded", function() {
        $("theGoogleForm").onsubmit = function() {
            var itms = [];
            //get checkboxes with name=selections
            //use a different name for other checkbox groups in the theGoogleForm (if any)
            //You can extend this to handle multiple checkbox groups by omitting the name parameter for later evaluation.
            var checks = this.getInputs("checkbox", "selections");
            checks.each(function(c) {
                if (c.checked) {
                    var itm = $F(c).split("|");
                    itms.push({ "item_name": itm[0], "item_description": itm[1], "item_price": itm[2], "item_weight": itm[3] });
                }
            });
            //Change 2 to any number of "required checked items" you need.
            //Change validation as necessary, currently, user must select exactly 2 items
            if (itms.length == 2) {
                var x = 1;
                var tgf = $("theGoogleForm");
                //build the elements based on selections
                itms.each(function(i) {
                    tgf.appendChild(e("item_name_" + x, i.item_name));
                    tgf.appendChild(e("item_description_" + x, i.item_description));
                    tgf.appendChild(e("item_price_" + x, i.item_price));
                    tgf.appendChild(e("item_weight_" + x, i.item_weight));
                    tgf.appendChild(e("item_weight_unit_" + x, "LB"));
                    tgf.appendChild(e("item_currency_" + x, "USD"));
                    tgf.appendChild(e("item_quantity_" + x, "1"));
                    x++;
                });
                //evaluation test was met, send to Google Checkout
                return true;
            } else {
                //evaluation test was not met, prevent form submission
                alert("A selection of 2 coffee blends is needed.\nYou selected " + itms.length + " blends.");
                return false;
            }
        };
    }
    );

    function e() {
        return new Element("input", { name: arguments[0], value: arguments[1], type: "hidden" });
    };
</script>

Tuesday 12 June 2012

How to Add Custom Field in Billing & Shipping addr

How to Add Custom Field in the Billing and Shipping Address of Onepage Checkout in Magento

I have created the module for adding the new custom field in the Billing and Shipping address of the Onepage checkout. All the code of the module is given below.

Step: – 1
Add Custom Field into Billing Address tab of the Onepage Checkout in Frontend.
app\design\frontend\[Package_name]\[Theme_name]\template\checkout\onepage\billing.phtml

Code:
<li class="wide">
    <label for="billing:jobtitle"><?php echo $this->__('Job Title') ?></label>
    <div class="input-box">
    <input type="text" id="billing:jobtitle" name="billing[jobtitle]" value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" title="<?php echo $this->__('Job Title') ?>" class="input-text" />
    </div>
    </li>


Step: – 2
Add Custom Field into Shipping Address tab of the Onepage Checkout in Frontend.
app\design\frontend\[Package_name]\[Theme_name]\template\checkout\onepage\shipping.phtml

Code:
<li class="wide">
    <label for="shipping:jobtitle"><?php echo $this->__('Job Title') ?></label>
    <div class="input-box">
    <input type="text" id="shipping:jobtitle" name="shipping[jobtitle]" value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" title="<?php echo $this->__('Job Title') ?>" class="input-text" onchange="shipping.setSameAsBilling(false);" />
    </div>
    </li>



Step: – 3
Add Custom Field into Customer Address Edit tab in Frontend.
app\design\frontend\[Package_name]\[Theme_name]\template\customer\address\edit.phtml


Code:
<li class="wide">
    <label for="jobtitle"><?php echo $this->__('Job Title') ?></label>
    <div class="input-box">
    <input type="text" name="jobtitle" id="jobtitle" title="<?php echo $this->__('Job Title') ?>" value="<?php echo $this->htmlEscape($this->getAddress()->getJobtitle()) ?>" class="input-text" />
    </div>
    </li>



Step: – 4
I have made some changes to the Address Templates in Magento Admin. if you are using Magento Version 1.3.2.4 then you have to made changes in the following file.

app/code/core/Mage/Customer/etc/config.xml

and if you are using Magento Upper Version than 1.3.2.4 then you can chage from the Admin panel of the Magento as given following path.

System->Configuration->Customers->Customer Configurations->Address Templates

Add following code in the “Text” Textarea.

Code:
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
     {{depend company}}{{var company}}{{/depend}}
     {{depend jobtitle}}{{var jobtitle}}{{/depend}}
     {{if street1}}{{var street1}}
     {{/if}}
     {{depend street2}}{{var street2}}{{/depend}}
     {{depend street3}}{{var street3}}{{/depend}}
     {{depend street4}}{{var street4}}{{/depend}}
     {{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
     {{var country}}
     T: {{var telephone}}
     {{depend fax}}F: {{var fax}}{{/depend}}



Step: – 5
I have made some changes to the Address Templates in Magento Admin. if you are using Magento Version 1.3.2.4 then you have to made changes in the following file.

app/code/core/Mage/Customer/etc/config.xml

and if you are using Magento Upper Version than 1.3.2.4 then you can chage from the Admin panel of the Magento as given following path.

System->Configuration->Customers->Customer Configurations->Address Templates

Add following code in the “HTML” Textarea.

Code:
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}<br/>
    {{depend company}}{{var company}}<br />{{/depend}}
    {{depend jobtitle}}{{var jobtitle}}<br />{{/depend}}
    {{if street1}}{{var street1}}<br />{{/if}}
    {{depend street2}}{{var street2}}<br />{{/depend}}
    {{depend street3}}{{var street3}}<br />{{/depend}}
    {{depend street4}}{{var street4}}<br />{{/depend}}
    {{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}<br/>
    {{var country}}<br/>
    {{depend telephone}}T: {{var telephone}}{{/depend}}
    {{depend fax}}<br/>F: {{var fax}}{{/depend}}



Step: – 6
I have made some changes to the Address Templates in Magento Admin. if you are using Magento Version 1.3.2.4 then you have to made changes in the following file.

app/code/core/Mage/Customer/etc/config.xml

and if you are using Magento Upper Version than 1.3.2.4 then you can chage from the Admin panel of the Magento as given following path.

System->Configuration->Customers->Customer Configurations->Address Templates

Add following code in the “PDF” Textarea.

Code:
{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}|
    {{depend company}}{{var company}}|{{/depend}}
    {{depend jobtitle}}{{var jobtitle}}|{{/depend}}
    {{if street1}}{{var street1}}
    {{/if}}
    {{depend street2}}{{var street2}}|{{/depend}}
    {{depend street3}}{{var street3}}|{{/depend}}
    {{depend street4}}{{var street4}}|{{/depend}}
    {{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}|
    {{var country}}|
    {{depend telephone}}T: {{var telephone}}{{/depend}}|
    {{depend fax}}<br />F: {{var fax}}{{/depend}}|


I have created the separate module for adding Custom Field into Billing and Shipping Address in Frontend and Backend of the Magento Store.

Step: – 7
app\etc\modules\Tdg_All.xml

First of all create file with above given name and put following code into that file.

Code:
<?xml version="1.0" encoding="UTF-8"?>
    <config>
     <modules>
     <Tdg_Check>
     <active>true</active>
     <codePool>local</codePool>
     </Tdg_Check>
     </modules>
    </config>



Step: – 8
app\code\local\Tdg\Check\etc\ config.xml


Code:
<?xml version="1.0" encoding="UTF-8"?>
    <config>
     <modules>
     <Tdg_Check>
     <version>1.0.0</version>
     </Tdg_Check>
     </modules>
     <admin>
     <fieldsets>
     <customer_dataflow>
     <jobtitle><billing>1</billing><shipping>1</shipping></jobtitle>
     </customer_dataflow>
     </fieldsets>
     </admin>
     <global>
     <models>
     <check>
     <class>Tdg_Check_Model</class>
     </check>
     </models>
     <resources>
     <check_setup>
     <setup>
     <module>Tdg_Check</module>
     </setup>
     <connection>
     <use>core_setup</use>
     </connection>
     </check_setup>
     <check_write>
     <connection>
     <use>core_write</use>
     </connection>
     </check_write>
     <check_read>
     <connection>
     <use>core_read</use>
     </connection>
     </check_read>
     </resources>
     <fieldsets>
     <sales_copy_order_billing_address>
     <jobtitle><to_order>*</to_order></jobtitle>
     </sales_copy_order_billing_address>
     <sales_copy_order_shipping_address>
     <jobtitle><to_order>*</to_order></jobtitle>
     </sales_copy_order_shipping_address>
     <sales_convert_quote_address>
     <jobtitle><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></jobtitle>
     </sales_convert_quote_address>
     <sales_convert_order_address>
     <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
     </sales_convert_order_address>
     <customer_address>
     <jobtitle><to_quote_address>*</to_quote_address></jobtitle>
     </customer_address>
     <checkout_onepage_billing>
     <jobtitle><to_customer>*</to_customer></jobtitle>
     </checkout_onepage_billing>
     </fieldsets>
     </global>
    </config>


Step: – 9
app\code\local\Tdg\Check\sql\check_setup\ mysql4-install-1.0.0.php


Code:
<?php
     /* @var $installer Mage_Customer_Model_Entity_Setup */
     $installer = $this;
     $installer->startSetup();
     /* @var $addressHelper Mage_Customer_Helper_Address */
     $addressHelper = Mage::helper('customer/address');
     $store = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    
     /* @var $eavConfig Mage_Eav_Model_Config */
     $eavConfig = Mage::getSingleton('eav/config');
    
     // update customer address user defined attributes data
     $attributes = array(
     'jobtitle' => array(
     'label' => 'Job Title',
     'backend_type' => 'varchar',
     'frontend_input' => 'text',
     'is_user_defined' => 1,
     'is_system' => 0,
     'is_visible' => 1,
     'sort_order' => 140,
     'is_required' => 1,
     'multiline_count' => 0,
     'validate_rules' => array(
     'max_text_length' => 255,
     'min_text_length' => 1
     ),
     ),
     );
    
     foreach ($attributes as $attributeCode => $data) {
     $attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
     $attribute->setWebsite($store->getWebsite());
     $attribute->addData($data);
     $usedInForms = array(
     'adminhtml_customer_address',
     'customer_address_edit',
     'customer_register_address'
     );
     $attribute->setData('used_in_forms', $usedInForms);
     $attribute->save();
     }
    
     $installer->run("
     ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
     ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `jobtitle` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
     ");
     $installer->endSetup();
    ?>


Custom tab in magento customer account page


The Customer edit page in the admin panel gives details about the customers account information,address,orders etc.

In some scenarios we may need to show our custom module contents related to the customer in a additional tab. To achieve this functionality we need to create a custom module and add our tab.

Step 1: We start with our config.xml file. In this file we are specifying an adminhtml xml layout file and a Block class for our custom module.

app/code/local/Mydons/etc/config.xml


<config>
<modules>
<Mydons_Customertab>
<version>0.1.0</version>
</Mydons_Customertab>
</modules>
<adminhtml>
<layout>
<updates>
<customertab>
<file>customertab.xml</file>
</customertab>
</updates>
</layout>
</adminhtml>
<global>
<blocks>
<customertab>
<class>Mydons_Customertab_Block</class>
</customertab>
</blocks>
</global>
</config>


Step 2: We need to create an adminhtml block class to show our new custom tab. This class
should extend the Mage_Adminhtml_Block_Template class and implement Mage_Adminhtml_Block_Widget_Tab_Interface to
get the tab related methods.

app/code/local/Mydons/Customertab/Block/Adminhtml/Customer/Edit/Tab/Action.php



<?php

/**
* Adminhtml customer action tab
*
*/
class Mydons_Customertab_Block_Adminhtml_Customer_Edit_Tab_Action
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{

public function __construct()
{
$this->setTemplate('customertab/action.phtml');

}

public function getCustomtabInfo(){

$customer = Mage::registry('current_customer');
$customtab='My Custom tab Action Contents Here';
return $customtab;
}

/**
* Return Tab label
*
* @return string
*/
public function getTabLabel()
{
return $this->__('Action Center');
}

/**
* Return Tab title
*
* @return string
*/
public function getTabTitle()
{
return $this->__('Action Tab');
}

/**
* Can show tab in tabs
*
* @return boolean
*/
public function canShowTab()
{
$customer = Mage::registry('current_customer');
return (bool)$customer->getId();
}

/**
* Tab is hidden
*
* @return boolean
*/
public function isHidden()
{
return false;
}

/**
* Defines after which tab, this tab should be rendered
*
* @return string
*/
public function getAfter()
{
return 'tags';
}

}
?>




Step 3: Now we need to create a layout file for our adminhtml.

app/design/adminhtml/default/default/layout/customertab.xml


<layout version="0.1.0">
<adminhtml_customer_edit>
<reference name="customer_edit_tabs">
<action method="addTab"><name>customer_edit_tab_action</name><block>customertab/adminhtml_customer_edit_tab_action</block></acti on>
</reference>
</adminhtml_customer_edit>
</layout>

Step 4: After creating the layout create adminthtml template file as shown below

app/design/adminhtml/default/default/template/customertab/action.phtml



<div id="customer_info_tabs_customer_edit_tab_action_content">
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-edit-form fieldset-legend">Action Tab</h4>
</div>
<div id="group_fields4" class="fieldset fieldset-wide">
<div class="hor-scroll">
<table class="form-list" cellspacing="0">
<tbody>
<tr>
<td>Custom Action Tab Contents Here</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>

Step 5: Finally we need to activate our module Activation file
app/etc/modules/Mydons_Customertab.xml.

<config>
<modules>
<Mydons_Customertab>
<active>true</active>
<codepool>local</codepool>
</Mydons_Customertab>
</modules>
</config>


the new tab display on click customer information -> new new tab display last



How to add custom tab on customer left navigation tab through custom module


 Way to create a tab in customer tab in magento


First to create custom module,
Then to create the yourmodule.xml file on
path:app/design/adminhtml/default/default/layout
<layout version="0.1.0">
    <adminhtml_customer_edit>
        <reference name="customer_edit_tabs">
            <action method="addTab"><name>custmotab</name><block>yourmodulename/adminhtml_yourmodulename</block></action>
        </reference>
   </adminhtml_customer_edit>
</layout>

After then go to the yourmodulename.php file
Path:app/code/local/yournamespace/yourmodulename/Block/Adminhtml
implements the following class
Mage_Adminhtml_Block_Widget_Tab_Interface
Then to add following code after __construct() function
public function getTabLabel()
    {
        return $this->__('yourcustomtabname');
    }
public function getTabTitle()
    {
        return $this->__('yourcustomtabtitle');
    }

Tuesday 5 June 2012

Some Important Functions: getModel(), getData(), getSingleton()

We will be looking into some important methods used frequently in Magento like getModel(), getData(), getSingleton()..
getModel() (To create an instance of a Model class) :
The getModel() function is used to create an instance of a Model class. For example
1$Product = Mage::getModel(’catalog/product’);
would basically tell Magento to create an instance of Mage_Catalog_Model_Product class. (ie. Mage/Catalog/Model/Product.php). If we now echo the get_class($Product) we would get the Mage_Catalog_Model_Product text displayed in our browser. You can use all methods defined in Product.php. Some useful methods include getName, getPrice, getTypeId, getStatus which we can execute like:
1echo ‘Product name: ‘. $Product->getName();
2echo ‘Product price: ‘ . $Product->getPrice();
However, the above code wont give you anything. You will not see any price displayed in the browser. This is because you haven’t loaded the product using load() method. We have to use the load method to load some data into our newly instantiated object.
1$Product->load(53);
where 53 is the product id.
getData() (To get relevant data from the object instance)
This function is used to get relevant data from the object instance. Let’s say you want to retrieve the SKU value. You can use some other method for that like getSku() method that needs to be executed on object of Product type or you can:
1echo $Product->getData(’sku’);
getData can be executed with or without any parameters passed to it. If you execute it without any parameters you get the array variable as a result. You can’t directly echo the array. You would have to map it to some field, like echo $arayVar['someField']. What are all the available fields? To find out you can do something like
1echo ‘<pre>’;
2print_r($Product->getData());
3echo ‘</pre>’;
You can get the sku of the product by:
1$ProductData = $Product->getData();
2echo $ProductData->sku;
or:
1echo $Product->getData(’sku’)
Another example:
1$ProductData = $Product->getData();
2$StockItem = $ProductData->stock_item;
Now your $StockItem variable is a object of type Mage_CatalogInventory_Model_Stock_Item and you can go ahead and use the same principle used until this point to find it’s methods and the data it holds.
First use the getModel to create the instance of object then you use getData to retrieve the data from that instance.
getSingleton():
One of the important architectural feature is it’s Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.
So when you are using getSingleton you are calling already instantiated object. So if you get the empty array as a result, it means the object is empty. Only the blueprint is there, but nothing is loaded in it.
Registry:

We had told that Magento provides global point of access to single instance of classes. How does Magento do it ?. This is done by using an array called registry in Mage.php.
Checkout the Mage.php file and you will see an array variable called _registry.
In order to provide global point of access for an instance of a class we must register the object into the registry array by using a function called register. For example:
1Mage::register(’events’, new Varien_Event_Collection());
2Mage::register(’config’, new Mage_Core_Model_Config());
Here is the code of the register() function:
1public static function register($key, $value, $graceful = false)
2{
3if(isset(self::$_registry[$key])) {
4if ($graceful) {
5return;
6}
7Mage::throwException(’Mage registry key “‘.$key.’” already exists’);
8}
9self::$_registry[$key] = $value;}
Basically what it does it registers an object with name as $key and the instance as $value into the registry array. You can also unregister an object using the unregister() function.
Whenever we want to call an object from the registry we use the registry() function. For example in the getConfig() method:
1public static function getConfig()
2{
3return Mage::registry(’config’);
4}
Previously we had registered an  instance of Mage_Core_Model_Config as ‘config’. The getConfig() function returns that instance. Here is the code for registry() function:
1public static function registry($key)
2{
3if (isset(self::$_registry[$key])) {
4return self::$_registry[$key];
5}
6return null;
7}
Analyzing the getModel() and getSingleton() code:
The getModel() and getSingleton() functions works by using this registry. Here is the code for the getModel() function:
1public static function getModel($modelClass=”, $arguments=array())
2{
3return Mage::getConfig()->getModelInstance($modelClass, $arguments);
4}
Basically it gets the Config object (This object is formed by parsing all the config.xml files, so it contains all config details of all moules) and then makes an instance of the model that is specified by the $modelClass argument.(for example if $modelClass is ‘catalog/product it creates an instance of Mage_Catalog_Model_Product)
Now take a look at the getSingleton Method:
1public static function getSingleton($modelClass=”, array $arguments=array())
2{
3$registryKey = ‘_singleton/’.$modelClass;
4if (!Mage::registry($registryKey)) {
5Mage::register($registryKey, Mage::getModel($modelClass, $arguments));
6}
7return Mage::registry($registryKey);
8}
Basically it checks wheter an instance of that class is already existing, if so return that instance else create a new instance and register it as ‘_singleton/$modelClass’.