Pages

Friday 4 July 2014

Magento Create Custom Shipping Method

In this blog post, we will see how to create a custom shipping method in magento. Creating a custom shipping method is very easy, you just need to know all the useful functions to be used inside the shipping method class to put in various configuration options. We will see in this blog many different use cases and functions to use in shipping methods.

 So let’s start of by creating the shipping method. I have used a module for this tutorial named Easylife_Ship. You can create your own module and change class names accordingly.

Module Name: Shipping Method Source

These are the primary areas where the shipping method shows up

1) Admin
2) Frontend

To achieve this there are 3 things required

1) config.xml entries
2) system.xml entries
3) shipping Module

So lets start of with the steps.

Step1
First thing when creating a shipping method is to decide the code for the shipping method. In the current module I am using the code for shipping method as “customshipping”. Next we will create the system.xml file for admin entries.

<?xml version="1.0" encoding="UTF-8"?>
<config>
 <sections>
  <carriers>
   <groups>
    <customshipping translate="label" module="ship">
     <label>Shipping Module</label>
     <frontend_type>text</frontend_type>
     <sort_order>99</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>1</show_in_store>
      </active>
      <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>
      <name translate="label">
       <label>Method Name</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>
      </name>
      <price translate="label">
       <label>Price</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>0</show_in_store>
      </price>
      <specificerrmsg translate="label">
       <label>Displayed Error Message</label>
       <frontend_type>textarea</frontend_type>
       <sort_order>4</sort_order>
       <show_in_default>1</show_in_default>
       <show_in_website>1</show_in_website>
       <show_in_store>1</show_in_store>
      </specificerrmsg>
      <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>
                            <can_be_empty>1</can_be_empty>
                        </specificcountry>
     </fields>
    </customshipping>
   </groups>
  </carriers>
 </sections>
</config>


So, let me explain this. first we always have to create our shipping method inside

<sections>
        <carriers>
            <groups>

As you can see the field have been created inside the ‘customshipping’ tag which is our shipping method code. Next, we have created fields for active,title,name,price. There fields are required for every shipping method created.

Magento Create Custom Shipping Method



 Next, I have included to more fields sallowspecific and specificcountry. This is required, when you want your shipping method to available for only few countries. To implement this restriction in the frontend (i.e suppose if user chooses India as shipping country our shipping doesnt show) we don’t have to do any more programming. Just included these two fields in the xml and country based restrictions start working automatically. Of course, we have to setup the country restriction in System Configuration in admin.

 Next, I have included specificerrmsg field. This field is used whenever there some error in our shipping method and we to show an error message to the user.


Step2
Next in our config.xml file of our module we add the below code directly inside the <config>

<default>
  <carriers>
       <customshipping>
            <active>1</active>
            <model>ship/carrier_customshipping</model>
            <title>Carrier Title</title>
            <name>Method Name</name>
            <price>5.00</price>
            <specificerrmsg>This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.</specificerrmsg>
         </customshipping>
      </carriers>
       </default>


As we know the <default> tag is used to assign default values to our system.xml fields created. So here we specify default values for active,title,name,price etc. The important field to notice here is the <model> which contains the path of our shipping model. This field is very important, or else the shipping method won’t show on frontend. After this step is over go to Admin -> System -> Configuration -> Shipping Methods and you should see your shipping method there with all the default values pre-filled.

Step3
Now all we need to do is to create our shipping model class. The model class name is Customshipping.php and is created Model/Carrier folder.

<?php
class Easylife_Ship_Model_Carrier_Customshipping extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface {
 protected $_code = 'customshipping';

 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
 {
  if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
   return false;
  }

  $price = $this->getConfigData('price'); // set a default shipping price maybe 0
  $price = 0;
  $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
  $result = Mage::getModel('shipping/rate_result');
  $show = true;
  if($show){

   $method = Mage::getModel('shipping/rate_result_method');
   $method->setCarrier($this->_code);
   $method->setMethod($this->_code);
   $method->setCarrierTitle($this->getConfigData('title'));
   $method->setMethodTitle($this->getConfigData('name'));
   $method->setPrice($price);
   $method->setCost($price);
   $result->append($method);

  }else{
   $error = Mage::getModel('shipping/rate_result_error');
   $error->setCarrier($this->_code);
   $error->setCarrierTitle($this->getConfigData('name'));
   $error->setErrorMessage($this->getConfigData('specificerrmsg'));
   $result->append($error);
  }
  return $result;
 }
 public function getAllowedMethods()
 {
  return array('customshipping'=>$this->getConfigData('name'));
 }
}


The above code is for our shipping model. The code is very basic, let go line by line
 

public function collectRates(Mage_Shipping_Model_Rate_Request $request)

This is the function which we need to implement. This function is called by magento for all the shipping methods to find out the shipping rates.

if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
  return false;
}

This line simply checks if the shipping method is enabled from admin.


$result = Mage::getModel('shipping/rate_result');
here we create the result object, which is always returned from the shipping method collectRate function.

$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setMethod($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethodTitle($this->getConfigData('name'));
$method->setPrice($this->getConfigData('price'));
$method->setCost($this->getConfigData('price'));
$result->append($method);

This code is used to return the shipping price. The code is quite clear.

$error = Mage::getModel('shipping/rate_result_error');
$error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('name'));
$error->setErrorMessage($this->getConfigData('specificerrmsg'));
$result->append($error);

The above code is used to return error message.

After this model is created, the shipping method should be working properly. It will show up on the checkout page and cart page.


Magento Create Custom Shipping Method


Here I have explained simply. Hopefully I have added all the information regarding shipping method. If there are any more scenarios on which you want code for shipping method let me know I will add it. If you have any queries feel free to leave a comment.

 

No comments:

Post a Comment