Monday, February 20, 2012

Magento: how to add new fields to customer account

First create xml file in etc/module named as: Bd_Customercustomfield
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Customercustomfield>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Bd_Customercustomfield>
  </modules>
</config>

Then create config file of your module with following code
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Customercustomfield>
      <version>0.1.0</version>
    </Bd_Customercustomfield>
  </modules>
  <global>
    <helpers>
      <customercustomfield>
        <class>Bd_Customercustomfield_Helper</class>
      </customercustomfield>
    </helpers>
<models>
 <customercustomfield>
<class>Bd_Customercustomfield_Model</class>
<resourceModel>customercustomfield_mysql4</resourceModel>
 </customercustomfield>
</models>
<resources>
 <customerattribute_setup>
<setup>
 <module>Bd_Customercustomfield</module>
 <class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
 <use>core_setup</use>
</connection>
 </customerattribute_setup>
 <customerattribute_write>
<connection>
 <use>core_write</use>
</connection>
 </customerattribute_write>
 <customerattribute_read>
<connection>
 <use>core_read</use>
</connection>
 </customerattribute_read>
</resources>
  </global>
</config>

At last create a mysql file mysql4-install-0.1.0.php with following code
<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "customer_mobile",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Mobile Number",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => true,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Customer Mobile Number"

));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "customer_mobile");

       
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
        $attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
        $attribute->save();
$installer->endSetup();
?>


How to learn Magento: Magento: 1.6 New Features

How to learn Magento: Magento: 1.6 New Features

Sunday, February 12, 2012

Magento: how to add states to countries in magento.


By default in magento only few countries have a state drop down when a country is selected like United States, in this tutorial we will see how to add state drop down for other countries as well.

The main table where are all the state values of a country are stored is directory_country_region and directory_country_region_name.
The fields of the table ‘directory_country_region’ are
1. region_id: auto-incremented id
2. country_id: the iso code of country (this code is taken from directory_country table)
3. code: unique code to be used for a state
4. default_name: name of the state

The second table is directory_country_region_name with fields
1. locale: website locale or language of the website
2. region_id: it’s a forign key from the previous table
3. name: name of the state for a specific language or locale.

The first table is required to add states to a country, the second table is only required if you want to state displayed in multiple languages.

So to add state to country Australia, we need to insert a row in the table directory_country_region

INSERT INTO  `directory_country_region` (
`region_id` ,
`country_id` ,
`code` ,
`default_name`
)
VALUES (
'0',  'AU',  'ACT',  'Australian Capital Territory');
Here ‘AU’ is the country code for Australia, ‘ACT’ is a user defined unique key and ‘Australian Capital Territory’ is the name of the state. Similarly we can add other states as well as
//start sql //
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'ACT', 'Australian Capital Territory');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'NSW', 'New South Wales');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'VIC', 'Victoria');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'QLD', 'Queensland');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'SA', 'South Australia');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'WA', 'Western Australia');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'TAS', 'Tasmania');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('AU', 'NT', 'Northern Territory');

Add also to add state to country India, we need to insert a row in the table directory_country_region

INSERT INTO  `directory_country_region` (
`region_id` ,
`country_id` ,
`code` ,
`default_name`
)
VALUES (
'0',  'IN',  'IN-DL',  'New Delhi'
);
Here ‘IN’ is the country code for India, ‘IN-DL’ is a user defined unique key and ‘New Delhi’ is the name of the state. Similarly we can add other states as well as


//start sql //
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-AN', 'Andaman and Nicobar');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Andaman and Nicobar');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-AP', 'Andhra Pradesh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Andhra Pradesh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-AR', 'Arunachal Pradesh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Arunachal Pradesh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-AS', 'Assam');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Assam');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-BR', 'Bihar');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Bihar');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-CH', 'Chandigarh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Chandigarh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-CG', 'Chhattisgarh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Chhattisgarh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-DN', 'Dadra and Nagar Haveli');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Dadra and Nagar Haveli');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-DD', 'Daman and Diu');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Daman and Diu');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-DL', 'Delhi');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Delhi');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-GA', 'Goa');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Goa');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-GJ', 'Gujarat');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Gujarat');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-HR', 'Haryana');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Haryana');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-HP', 'Himachal Pradesh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Himachal Pradesh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-JK', 'Jammu and Kashmir');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Jammu and Kashmir');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-JH', 'Jharkhand');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Jharkhand');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-KA', 'Karnataka');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Karnataka');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-KL', 'Kerala');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Kerala');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-LD', 'Lakshadweep');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Lakshadweep');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-MP', 'Madhya Pradesh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Madhya Pradesh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-MH', 'Maharashtra');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Maharashtra');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-MN', 'Manipur');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Manipur');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-ML', 'Meghalaya');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Meghalaya');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-MZ', 'Mizoram');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Mizoram');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-NL', 'Nagaland');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Nagaland');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-OR', 'Orissa');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Orissa');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-PY', 'Puducherry');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Puducherry');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-PB', 'Punjab');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Punjab');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-RJ', 'Rajasthan');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Rajasthan');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-SK', 'Sikkim');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Sikkim');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-TN', 'Tamil Nadu');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Tamil Nadu');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-TR', 'Tripura');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Tripura');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-UK', 'Uttarakhand');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Uttarakhand');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-UP', 'Uttar Pradesh');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'Uttar Pradesh');
INSERT INTO `directory_country_region` (`country_id`, `code`, `default_name`) VALUES ('IN', 'IN-WB', 'West Bengal');
INSERT INTO `directory_country_region_name` (`locale`, `region_id`, `name`) VALUES ('en_US', LAST_INSERT_ID(), 'West Bengal');

//end sql //

Magento: 1.6 New Features


In case your thinking of upgrading magento to version 1.6.1 you can see what all new features you will get.
In this blog post we will see 3 major features introduced

Minimum Advertised Price
Persistent Shopping Cart
Added two-step password reset flow

Minimum Advertised Price
This feature is useful if your product manufacture has established a Manufacturer’s Suggested Retail Price or ‘MSRP’, and you want to sell the product at a price lower than MSRP. What this feature basically does is hide’s the product price display in your catalog and only shows this during product purchase. To setup this feature you need to first configure settings at System -> Configuration -> Sales -> Minimum Advertised Price.
Minimum Advertised Price System Configuration

Here is an explanation of the settings

Enable MAP: This is used to turn on/off the MAP feature.
Apply MAP (Default Value): If this option is selected as Yes, this means all product price will get hidden. If you select No here, then you need to individually select which product to apply the MAP on.
Display Actual Price: Here you can select at which step you want to show the customer the product price. In Cart means customer can see product price when products have been added to cart. Before Order Confirmation means product prices are shown at last step of checkout. On Gesture means product prices are shown in catalog pages but only when user clicks on link which shows price in a popup.
Default Popup Text Message: To configure the display text
Default “What’s This” Text Message: To configure the display text
Next set of configuration can be done in the product edit page. It is available in the Price tab.

Minimum Advertised Price Product Admin

Here is the explanation of the settings

Apply MAP: Here you can select Yes/No or use the default configuration.
Display Actual Price: This setting is same as the Global setting to display actual price, except you can make setting for each product here.
Manufacturer’s Suggested Retail Price: This is the value of the MSRP field, here you can mention the price offered by the manufacturer.

Persistent Shopping Cart
This feature basically adds a Remember Me option to the login and registration page. You can enable this feature from System -> Configuration -> Persistent Shopping Cart –> General Options.
Persistent Shopping Cart

Here is the explanation of the settings
Persistence Lifetime (seconds): Here you can set for long to remember a users login
Enable “Remember Me”: To show remember me checkout on login and registration pages
“Remember Me” Default Value: Weather remember me should be checked or unchecked by default
Clear Persistence on Log Out: To clear cookie values when user logs out
Persist Shopping Cart: Weather to remember shopping cart items of a user


Added two-step password reset flow
In this feature what magento has implemented is that, when a user click on the forgot password field with his email address, he gets a forgot password link in his email address from where he can get the new password. In older magento implementations, clicking on forgot password would directly change the password of the user, so anybody could change password of any user. But now due to the 2 step process only correct users can change their password.

Thursday, February 9, 2012

Magento: How to get actual price and special price of a product?


Here is a quick and useful code on getting actual price and special price of any product in Magento. The actual price is the real price assigned to the product and special price is the price after any discount is applied to the product.

Loading Product

$_productId = 52;
$_product = Mage::getModel('catalog/product')->load($_productId);

Get Actual Price

// without currency sign
$_actualPrice = $_product->getPrice();

// with currency sign
$_formattedActualPrice = Mage::helper('core')->currency($_product->getPrice(),true,false);
Get Special Price

// without currency sign
$_specialPrice = $_product->getFinalPrice();

// with currency sign
$_formattedSpecialPrice = Mage::helper('core')->currency($_product->getFinalPrice(),true,false);

http://blog.chapagain.com.np/magento-how-to-get-actual-price-and-special-price-of-a-product/

Magento: Show/Hide website to search engines by adjusting robots meta tag


The Robots META Tag is meant to provide users who cannot upload or control the /robots.txt file at their websites, with a last chance to keep their content out of search engine indexes and services.

<meta name="robots" content="robots-terms">

The content=”robots-terms” is a comma separated list used in the Robots META Tag that may contain one or more of the following keywords without regard to case: noindex, nofollow, all, index and follow.

noindex = Page may not be indexed by a search service.
nofollow = Robots are not to follow links from this page.

You can find more about the robots meta tag over here: http://www.seoconsultants.com/meta-tags/robots/

There is an easy way in Magento to adjust robots meta tags. You can simply choose from admin settings.

Go to System -> Configuration -> Design -> HTML Head -> Default Robots

You have the following options to choose:

- index, follow
- noindex, follow
- index, nofollow
- noindex, nofollow

If you have a live site (production site) then choose ‘index, follow‘. If you have a test site (development site) then choose ‘noindex, nofollow‘.

Magento: Show/Hide Demo Store Notice


A demo store is where there are products but the order done by customer is not processed. If your website is in development mode then it is better to enable demo store notice.
Enabling demo store notice in Magento is very simple. You just need to select an option in configuration settings in admin panel.
Go to System -> Configuration -> Design -> HTML Head -> Display Demo Store Notice
By default, it is set as ‘No’. To enable demo store notice, select ‘Yes’.
Now, you will see a notice bar at top of your page saying ‘This is a demo store. Any orders placed through this store will not be honored or fulfilled.

Magento: How to get product stock quantity & other stock information?


Here is a quick code to get any product’s stock information like quantity (qty), minimum quantity (min_qty), stock availability (is_in_stock), minimum and maximum sale quantity (min_sale_qty and max_sale_qty), etc.

First load the product. Product can be loaded in different ways. Here are the two different ways to load any product in Magento:-


1. Load product by product ID

$id = 52;
$_product = Mage::getModel('catalog/product')->load($id);
2. Load product by SKU

$sku = "microsoftnatural";
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
Now, get stock information for the loaded product.

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
You can check stock data in this way:-

echo "<pre>"; print_r($stock->getData()); echo "</pre>";
Or, you can print individually like this:-

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();