Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a custom shipping method in Magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 149
    Comment on it

    Magento supports various shipping methods by default like flat rate, free shipping etc. To view the list of shipping methods available go to "System->Configuration". Under left menu select "Sales" section and then select "Shipping Methods" tab. All available shipping methods will be shown on right side.

    First, let's understand the file structure we need to create for shipping module -

    1) app/etc/modules/Custom_Shippingmethod.xml
    2) app/code/local/Custom/Shippingmethod/etc/config.xml
    3) app/code/local/Custom/Shippingmethod/etc/system.xml
    4) app/code/local/Custom/Shippingmethod/Model/Carrier.php
    5) app/code/local/Custom/Shippingmethod/Helper/Data.php

    Now we create files step by step -

    1) First we will create module file "Custom_Shippingmethod.xml" in "app/etc/modules/" folder and put the following content in it.

    <?xml version="1.0"?>
    <config>
        <modules>
            <Custom_Shippingmethod>
                <active>true</active>
                <codePool>local</codePool>
                <depends>
                    <Mage_Shipping />
                </depends>
            </Custom_Shippingmethod>
        </modules>
    </config>
    

    2) Now the module configuration file "config.xml" in "app/code/local/Custom/Shippingmethod/etc/" folder with the following content.

    <?xml version="1.0"?>
    <config>
        <modules>
            <Custom_Shippingmethod>
                <version>0.1.0</version>
            </Custom_Shippingmethod>
        </modules>
        <global>
            <models>
                <custom_shippingmethod>
                    <class>Custom_Shippingmethod_Model</class>
                </custom_shippingmethod>
            </models>
            <helpers>
                <custom_shippingmethod>
                    <class>Custom_Shippingmethod_Helper</class>
                </custom_shippingmethod>
            </helpers>
        </global>
        <default>
            <carriers>
                <custom_shippingmethod>
                    <active>1</active>
                    <sort_order>10</sort_order>
                    <model>custom_shippingmethod/carrier</model>
                    <title>Custom Shipping</title>
                    <sort_order>100</sort_order>
                    <sallowspecific>1</sallowspecific>
                </custom_shippingmethod>
            </carriers>
        </default>
    </config>
    

    3) To make it configurable from back-end we need to create "system.xml" file in "app/code/local/Custom/Shippingmethod/etc/" folder with the following content.

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <sections>
            <carriers>
                <groups>
                    <customshippingmethod translate="label" module="customshippingmethod">
                        <label>Custom Shipping Carrier</label>
                        <sortorder>2</sortorder>
                        <showindefault>1</showindefault>
                        <showinwebsite>0</showinwebsite>
                        <showinstore>0</showinstore>
                        <fields>
                     <label>Enabled</label>
                                <frontendtype>select</frontendtype>
                                <source_model>adminhtml/system_config_source_yesno</source_model>
                                <sort_order>1</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>0</show_in_website>
                                <show_in_store>0</show_in_store>
                            </active>
                            <title translate="label">
                                <label>Carrier title</label>
                                <frontend_type>text</frontend_type>
                                <sort_order>20</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>0</show_in_website>
                                <show_in_store>0</show_in_store>
                            </title>
                            <sallowspecific translate="label">
                                <label>Available for specific countries only</label>
                                <frontend_type>select</frontend_type>
                                <frontend_class>shipping-applicable-country</frontend_class>                    <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
                                <sort_order>30</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>0</show_in_website>
                                <show_in_store>0</show_in_store>
                            </sallowspecific>
                            <specificcountry translate="label">
                                <label>Ship to Specific Countries</label>
                                <frontend_type>multiselect</frontend_type>
                                <sort_order>31</sort_order>
                                <source_model>adminhtml/system_config_source_country</source_model>
                                <show_in_default>1</show_in_default>
                                <show_in_website>0</show_in_website>
                                <show_in_store>0</show_in_store>
                                <can_be_empty>1</can_be_empty>
                            </specificcountry>
                        </fields>
                    </custom_shippingmethod>
                </groups>
            </carriers>
        </sections>
    </config>
    

    4) Now , we will create model file "Carrier.php" to interact with database in "app/code/local/Custom/Shippingmethod/Model/" folder with the following content.

    <?php
    class Custom_Shippingmethod_Model_Carrier
        extends Mage_Shipping_Model_Carrier_Abstract
        implements Mage_Shipping_Model_Carrier_Interface
    {
    
        protected $_code = 'custom_shippingmethod';
    
    
        public function collectRates(Mage_Shipping_Model_Rate_Request $request)
        {
    
            $result = Mage::getModel('shipping/rate_result');
    
            $expressAvailable = true;
    
    
            $result->append($this->_getStandardRate());
    
            return $result;
        }
    
        public function getAllowedMethods()
        {
            return array( 'custom' => 'Custom Delivery Rate' );
        }
    
    
        protected function _getStandardRate()
        {
            $rate = Mage::getModel('shipping/rate_result_method');
    
            $rate->setCarrier($this->_code);
            $rate->setCarrierTitle($this->getConfigData('title'));
            $rate->setMethod($this->_code);
            $rate->setMethodTitle($this->getConfigData('title'));
            $rate->setPrice(2.00);
            $rate->setCost(0);
    
            return $rate;
        }
    }
    

    5) Now, we will create "Data.php" file in "app/code/local/Custom/Shippingmethod/Helper/" folder with the following content.

    <?php
    class Custom_Shippingmethod_Helper_Data extends Mage_Core_Helper_Abstract
    {
    }
    

    Now, after setup from backend we can have the shipping option available on frontend.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: