Need help buying now? +48-22-219-5163 +1-917-720-3250
My Cart

Your Cart is Empty

Need help buying now?
+48-22-219-5163 +1-917-720-3250

Shipping module for Magento

Shipping module for Magento

Many developers state that Magento is a very complicated system and it is almost impossible for a user to create extensions for it by themselves. Today we are going to discuss how to create a shipping module for Magento, what should be the basic compounds and construction of this system element.

Principle basics of a shipping module

It is extremely important for a customer not only to find the necessary product in an online store but to also receive it without any problems. Naturally, it is no less important for the store owners to arrange the process of shipment properly. By default, Magento system recognizes such world-wide known shipping companies:

1. UPS

2. USPS

3. FedEx

4. DHL

In order to add a specific shipping company, e.g. Canada postal service, you need to create your own shipping module. Of course, in most cases this task should be handled by Magento development team but we will show you how to do that by yourself.

Module configuration initialization

During a new Magento module creation the most important task is writing of configuration files, as with any other module. This recipe will describe the steps of creating a configuration file for our new shipping module.

Launch your PHP editor and open your Magento admin. Now you need to create a folder called ‘Packt’ inside app/code/local directory. Inside the newly created folder create another folder called ‘Myshipping’. Inside this folder create two more folders – ‘Model’ and ‘etc’. In the folder ‘etc’ create a file called config.xml with the following code:

<?xml version="1.0"?>
<config>
<modules>
<Packt_Myshipping>
<version>1.0.0</version>
<depends>
<Mage_Shipping />
</depends>
</Packt_Myshipping>
</modules>
<global>
<models>
<myshipping>
<class>Packt_Myshipping_Model</class>
</myshipping>
</models>
<resources>
<myshipping_setup>
<setup>
<module>Packt_Myshipping</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</myshipping_setup>
</resources>
</global>
<default>
<carriers>
<myshipping>
<active>1</active>
<model>myshipping/carrier</model>
</myshipping>
</carriers>
</default>
</config>

 

Once the code is pasted save the file and close it. Now you need to create one more file called System.xml inside the folder ‘etc’. This file will contain quite a long code string so you can download it on the official site for more convenience.

The next code part is directly related to the configuration of Myshipping module, particularly to the sorting order, administrator sign and other additional configuration elements.

<?xml version="1.0"?>
<config>
<sections>
<carriers>
<groups>
<myshipping translate="label" module="shipping">
<label>Canada Post</label>
<frontend_type>text</frontend_type>
<sort_order>13</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>

 

The block ‘fields’ contains the settings for all fields that can be found in the admin panel. These are products’ visibility to customers and administrators, a store sign, products sorting and other configurations.

<fields>
<account translate="label">
<label>Account number</label>
<frontend_type>text</frontend_type>
<sort_order>7</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</account>
<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>
<contentdesc translate="label">
<label>Package Description</label>
<frontend_type>text</frontend_type>
<sort_order>12</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</contentdesc>

 

Remember that, if the option ‘free_shipping_enable’ is enabled in the standard Magento shipping settings, then the platform will check free_shipping_subtotal in order to be able to verify the details of Shopping Cart Price Rule.

<free_shipping_enable translate="label">
<label>Free shipping with minimum order
amount</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_
enabledisable</source_model>
<sort_order>21</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</free_shipping_enable>
<free_shipping_subtotal translate="label">
<label>Minimum order amount for free
shipping</label>
<frontend_type>text</frontend_type>
<sort_order>22</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</free_shipping_subtotal>
<dutiable translate="label">
<label>Shipment Dutiable</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno
</source_model>
<sort_order>13</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</dutiable>
<gateway_url translate="label">
<label>Gateway URL</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>
</gateway_url>
<handling_type translate="label">
<label>Calculate Handling Fee</label>
<frontend_type>select</frontend_type>
<source_model>shipping/source_handlingType
</source_model>
<sort_order>10</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_action translate="label">
<label>Handling Applied</label>
<frontend_type>select</frontend_type>
<source_model>shipping/source_handlingAction
</source_model>
<sort_order>11</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</handling_action>
<handling_fee translate="label">
<label>Handling fee</label>
<frontend_type>text</frontend_type>
<sort_order>12</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</handling_fee>
<max_package_weight translate="label">
<label>Maximum Package Weight (Please consult your
shipping carrier for maximum supported shipping
weight)</label>
<frontend_type>text</frontend_type>
<sort_order>13</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</max_package_weight>
<id translate="label">
<label>Access ID</label>
<frontend_type>text</frontend_type>
<backend_model>adminhtml/system_config_backend_
encrypted</backend_model>
<sort_order>5</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</id>
<password translate="label">
<label>Password</label>
<frontend_type>text</frontend_type>
<backend_model>adminhtml/system_config_backend_
encrypted</backend_model>
<sort_order>6</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</password>
<shipping_intlkey translate="label">
<label>Shipping key (International)</label>
<frontend_type>text</frontend_type>
<backend_model>adminhtml/system_config_backend_
encrypted</backend_model>
<sort_order>8</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</shipping_intlkey>
<shipping_key translate="label">
<label>Shipping key</label>
<frontend_type>text</frontend_type>
<backend_model>adminhtml/system_config_
backend_encrypted</backend_model>
<sort_order>8</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</shipping_key>
<sort_order translate="label">
<label>Sort order</label>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</sort_order>
<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>
<sallowspecific translate="label">
<label>Ship to applicable countries</label>
<frontend_type>select</frontend_type>
<sort_order>90</sort_order>
<frontend_class>shipping-applicablecountry</
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>1</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>1</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>1</show_in_store>
</showmethod>
<specificerrmsg translate="label">
<label>Displayed Error Message</label>
<frontend_type>textarea</frontend_type>
<sort_order>80</sort_order>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</specificerrmsg>
</fields>
</myshipping>
</groups>
</carriers>
</sections>
</config>
<show_in_default>1</show_in_default>

 

This is the last stage of configuration of a Magento module. You need to create a new module in folder app/etc/modules/Packt_Myshipping and add the following code into it:

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

 

What was done

It is known that XML files are the basic components of Magento CMS. They are used to create any Magento modules including GoMage extensions. The code taken from http://magento-forum.ru/ is the code for two main files - config.xml and System.Xml. But, beside those files, it is also very important to create an extension adapter that will command the class Mage_Shipping_Model_Carrier_Abstract. For that you need to create a file Carrier.php in folder app/code/local/Packt/Myshipping/Model and copy the following code into it:

<?php
class Packt_Myshipping_Model_Carrier extends Mage_Shipping_Model_
Carrier_Abstract
{
/**
* unique identifier for our shipping module
* @var string $_code
*/
protected $_code = 'myshipping';
/**
* Collect rates for this shipping method based on information
in $request
*
* @param Mage_Shipping_Model_Rate_Request $data
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request
$request)
{
if (!$this->getConfigData('active')) {
Mage::log('The ' . $this->_code . 'my shipping module is not
active.');
return false;
}
$handling = $this->getConfigData('handling_fee');
$result = Mage::getModel('shipping/rate_result');
foreach ($request as $method) {
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($method['code']);
$method->setMethodTitle($method['title']);
$method->setCost($method['amount']);
$method->setPrice($method['amount'] + $handling);
$result->append($method);
}
return $result;
}
}

 

Save the changes you have made.

It is notable that this file will work even for duty calculation and that sum will be added to the shipping form. In order to set your positions you only need to configure system.xml properly. Then you can enable your new extension.

In order to enable a new shipping module in Magento you should go to the admin panel, then to System - Configuration Advanced - Advanced page. On the page that opens check if Packt_Myshipping is enabled. Then go back to System - Shipping methods, which is under Configuration =>Sales. In the code sample we set ‘Canada Post’ as the method name so we need to search for it now. Enter the search request into the corresponding window. Make sure this method is also enabled. After this work is done, contact your shipment manager and ask them to check if everything is displayed properly. Make sure you save all settings.

In order to make the module visible on the site you need to go to the admin panel, Shipping Methods section. Fill in the information for Canada Post and save the changes. After that everything should work. Of course, you will also need to check the functionality of your module by making a test shipment through it.

Newsletter