Thursday, 11 July 2013

Magento Custom Shipping Module

In this blog i have describe how to create custom shipping module in magento.

I have used one method for this module named "My_Ship".

There are 3 primary things required for create this module.
1) system.xml
2) config.xml
3) Shipping Module

Step 1


Create system.xml file in app\code\local\Deb\Ship\etc folder, then use the below code in this file.


<?xml version="1.0"?>
<config>
   <sections>
    <carriers>
        <groups>
           <express translate="label">
    <label>Deb Express Shipping Method</label>
    <frontend_type>text</frontend_type>
    <sort_order>1</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    <fields>
        <active translate="label">
            <label>Enabled</label>
            <frontend_type>select</frontend_type>
            <source_model>adminhtml/system_config_source_yesno</source_model>
            <sort_order>1</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </active>
        <name translate="label">
            <label>Method name</label>
            <frontend_type>text</frontend_type>
            <sort_order>3</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </name>
        <price translate="label">
            <label>Price</label>
            <frontend_type>text</frontend_type>
            <sort_order>5</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </price>
        <handling_type translate="label">
            <label>Calculate Handling Fee</label>
            <frontend_type>select</frontend_type>
            <source_model>shipping/source_handlingType</source_model>
            <sort_order>7</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </handling_type>
        <handling_fee translate="label">
            <label>Handling Fee</label>
            <frontend_type>text</frontend_type>
            <sort_order>8</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </handling_fee>
        <sort_order translate="label">
            <label>Sort order</label>
            <frontend_type>text</frontend_type>
            </sort_order><sort_order>100</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        <title translate="label">
            <label>Title</label>
            <frontend_type>text</frontend_type>
            <sort_order>2</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </title>
        <type translate="label">
            <label>Type</label>
            <frontend_type>select</frontend_type>
            <source_model>adminhtml/system_config_source_shipping_flatrate</source_model>
            <sort_order>4</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </type>
        <sallowspecific translate="label">
            <label>Ship to applicable countries</label>
            <frontend_type>select</frontend_type>
            <sort_order>90</sort_order>
            <frontend_class>shipping-applicable-country</frontend_class>
            <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</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>91</sort_order>
            <source_model>adminhtml/system_config_source_country</source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </specificcountry>
        <showmethod translate="label">
            <label>Show method if not applicable</label>
            <frontend_type>select</frontend_type>
            <sort_order>92</sort_order>
            <source_model>adminhtml/system_config_source_yesno</source_model>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>0</show_in_store>
        </showmethod>
        <specificerrmsg translate="label">
            <label>Displayed Error Message</label>
            <frontend_type>textarea</frontend_type>
            <sort_order>80</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
        </specificerrmsg>
    </fields>
</express>

</groups>
        </carriers>
    </sections>
</config>

Step 2


Create one config.xml file in app\code\local\Deb\Ship\etc folder and used the below code.



<?xml version="1.0"?>
<config>
  <modules>
    <Deb_Ship>
      <version>0.1.0</version>
    </Deb_Ship>
  </modules>
  <global>
    <models>
      <ship>
        <class>Deb_Ship_Model</class>
      </ship>
    </models>  
  </global>
  <default>
<carriers>
<express>
            <active>0</active>
            <sallowspecific>0</sallowspecific>
            <model>shipping/carrier_express</model>
            <name>Express</name>
            <price>5.00</price>
            <title>Express</title>
            <type>I</type>
            <specificerrmsg>This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.</specificerrmsg>
            <handling_type>F</handling_type>
    </express>

</carriers>
  </default>
</config>

Step 3


Create one Express.php file in app\code\local\Deb\Ship\Model\Carrier  folder and used the below code.




<?php
class Deb_Ship_Model_Carrier_Express
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{
    protected $_code = 'express';
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
        $freeBoxes = 0;
        if ($request->getAllItems()) {
            foreach ($request->getAllItems() as $item) {
                if ($item->getFreeShipping() && !$item->getProduct()->isVirtual()) {
                    $freeBoxes+=$item->getQty();
                }
            }
        }
        $this->setFreeBoxes($freeBoxes);
        $result = Mage::getModel('shipping/rate_result');
        if ($this->getConfigData('type') == 'O') { // per order
            $shippingPrice = $this->getConfigData('price');
        } elseif ($this->getConfigData('type') == 'I') { // per item
            $shippingPrice = ($request->getPackageQty() * $this->getConfigData('price')) - ($this->getFreeBoxes() * $this->getConfigData('price'));
        } else {
            $shippingPrice = false;
        }
        $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
        if ($shippingPrice !== false) {
            $method = Mage::getModel('shipping/rate_result_method');
            $method->setCarrier('express');
            $method->setCarrierTitle($this->getConfigData('title'));
            $method->setMethod('express');
            $method->setMethodTitle($this->getConfigData('name'));
            if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
                $shippingPrice = $this->getConfigData('price');
            }
            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);
            $result->append($method);
        }
        return $result;
    }
    public function getAllowedMethods()
    {
        return array('express'=>$this->getConfigData('name'));
    }
}

?>




Then create xml file for active this module in magento store.

Create Deb_Ship.xml in app\etc\modules and used the below code.


<?xml version="1.0"?>
<config>
    <modules>
        <Deb_Ship>
            <active>true</active>
            <codePool>local</codePool>
        </Deb_Ship>
    </modules>
</config>

enjoy................






No comments:

Post a Comment

Thanks for your comments.