Apr 11, 2012

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

Sometimes we need to add the custom fields in to the Onepage checkout process, here I am adding a custom field "comments" into the "Shipping Information" tab of Onepage checkout process and storing the values into the tables
"sales_flat_quote_address" & "sales_flat_order_address".

Please see the example below for better understanding:

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

<li class="control">
  <div>
     <label for="shipping:comments"><?php echo $this->__('Comments') ?  ></label><br />
    <input type="text"  name="shipping[comments]" id="comments" title="Comments" value="" class="input-  text" maxlength="20" />
 </div>
</li>

Creating custom module name as "Customization"
Step2: Create app\etc\modules\Namespace_ Customization.xml
<Namespace_Customization>
            <active>true</active>
            <codePool>local</codePool>
</Namespace_Customization>

Step3: create a xml file
app\code\local\Namespace\Customization\etc\ config.xml
<config>
    <modules>
        <Namespace_Customization>
            <version>0.1.0</version>
        </Namespace_Customization>
    </modules>
    <global>
  <models>
            <namespace_customization>
                <class>Namespace_Customization_Model</class>
                <resourceModel>namespace_customization_mysql4</resourceModel>
            </namespace_customization>
   <namespace_customization_mysql4>
    <class>Namespace_Customization_Model_Mysql4</class>
   </namespace_customization_mysql4> 
        </models>
  <resources>
            <namespace_customization_setup>
                <setup>
                    <module>Namespace_Customization</module>
     <class>Namespace_Customization_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </namespace_customization_setup>
            <namespace_customization_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </namespace_customization_write>
            <namespace_customization_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </namespace_customization_read>
        </resources>
  <blocks>
            <namespace_customization><class>Namespace_Customization_Block</class></namespace_customization>
        </blocks>
        <helpers>
            <namespace_customization><class>Namespace_Customization_Helper</class></namespace_customization>
        </helpers>      
 <fieldsets>
      
<sales_convert_quote_address>
   <comments><to_order_address>*</to_order_address></comments >
  </sales_convert_quote_address>
  <sales_convert_order_address>
  < comments ><to_quote_address>*</to_quote_address></comments >
              </sales_convert_order_address>
 </fieldsets>
    </global>
    <default>
        <cart>
            <email>
                <email_identity>general</email_identity>
                <email_template>cart_email_email_template</email_template>
            </email>
        </cart>
    </default>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <namespace_customization after="Mage_Checkout">Namespace_Customization</namespace_customization>
                    </modules>
                </args>
            </checkout>
        </routers>
        <routers>
            <customization>
                <use>standard</use>
                <args>
                    <module>Namespace_Customization</module>
                    <frontName>customization</frontName>
                </args>
            </customization>
        </routers>
 
    </frontend> 
</config>

Step4: create a sql setup file app\code\local\Namespace\Customization\sql\namespace_customization_setup\mysql4-install-0.1.0.php
Add the content below in file.

<?php
$installer = $this;
/* @var $installer Mage_Sales_Model_Mysql4_Setup */

$entityAttributesCodes = array(
    'comments' => 'varchar'  
);
foreach ($entityAttributesCodes as $code => $type) {
    $installer->addAttribute('quote_address', $code, array('type' => $type, 'default' => '0', 'user_defined' => false));
    $installer->addAttribute('order_address', $code, array('type' => $type, 'default' => '0', 'user_defined' => false));
    }

Step 5: Override the model file into your local app\code\local\Namespace\Checkout\Model\Type\Onepage.php
and add the below content.

class Namespace_Checkout_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage
{
    /**
     * Save checkout shipping address
     *
     * @param   array $data
     * @param   int $customerAddressId
     * @return  Mage_Checkout_Model_Type_Onepage
     */
    public function saveShipping($data, $customerAddressId)
    {
        if (empty($data)) {
            return array('error' => -1, 'message' => $this->_helper->__('Invalid data.'));
        }
        $address = $this->getQuote()->getShippingAddress();
        ...................
            .....................Intermediate code ........
                ........................
             
        if (!empty($customerAddressId)) {
            $customerAddress = Mage::getModel('customer/address')->load($customerAddressId);
            if ($customerAddress->getId()) {
              ...................
                ..................... Intermediate code ........
                ........................
                if ($addressErrors !== true) {
                    return array('error' => 1, 'message' => $addressErrors);
                }
                // comments: set comments field value              
                $address->setSpecDelComment2(empty($data['comments']) ? '' : $data['comments']);          
            }
        } else {
            $addressForm->setEntity($address);
            ...................
                ..................... Intermediate code ........
                ........................
            $addressForm->compactData($addressData);          
            // comments: set comments filed value
            $address->setSpecDelComment2(empty($data['comments']) ? '' : $data['comments']);
        }

        ...................
                ..................... Intermediate code ........
                ........................
        $this->getQuote()->collectTotals()->save();
        ...................
                ..................... Intermediate code ........
                ........................

        return array();
    }

Step 6: Refresh the Cache & ur done.









How to sort & limit a collection in Magento?

We can sort & set limit to a collection in Magento.
Please refer the below example for better understanding.

For Example:
$collection = Mage::getModel('news/news')->getCollection();
$collection->>addAttributeToFilter('store_id', $storeId);

$collection->setOrder('news_id','ASC');
$collection->setOrder('update_time','DESC');
$collection->setPageSize(5);
$collection->count();

Description:
Step 1: We have created a "news" model collection i.e ($collection).
Step2: Filtering the collection by "store_id".
Step3: Sorting the collection by "news_id" in Ascending order.
Step4: Sorting the collection by "update_time" in Descending order.
Step5: Set limit of 5 records on collection.
Step6: Forces the collection to load.

Output Query like below:

SELECT `main_table`.* FROM `news` AS `main_table` WHERE (store_id = '1')
ORDER BY news_id ASC, update_time DESC, news_id  ASC, update_time DESC LIMIT 5


Apr 4, 2012

MAGEREVERSE is an online Database Diagram Tool dedicated to Magento eCommerce CE Edition.

The one of the OSSOM, simple and easy tool of Magento DB structure, I loved it.

MAGEREVERSE is an online Database Diagram Tool dedicated to Magento eCommerce CE Edition.
It is very simple to understand the Magento CE database tables structure and their relative tables realtions.

Click here for more details.

Thanks a lot to all team and specially to " Selim KHEMISSI "....