12 min read

(For more resources related to this topic, see here.)

Shipping ordered products to customers is one of the key parts of the e-commerce flow. In most cases, a shop owner has a contract with a shipping handler where everyone has their own business rules.

In a standard Magento, the following shipping handlers are supported:

  • DHL
  • FedEx
  • UPS
  • USPS

If your handler is not on the list, have a look if there is a module available at Magento Connect. If not, you can configure a standard shipping method or you can create your own, which we will do in this article.

Initializing module configurations

In this recipe, we will create the necessary files for a shipping module, which we will extend with more features using the recipes of this article.

Getting ready

Open your code editor with the Magento project. Also, get access to the backend where we will check some things.

How to do it…

The following steps describe how we can create the configuration for a shipping module:

  1. Create the following folders:

    • app/code/local/Packt/
    • app/code/local/Packt/Shipme/
    • app/code/local/Packt/Shipme/etc/
    • app/code/local/Packt/Shipme/Model/
    • app/code/local/Packt/Shipme/Model/Carrier
  2. Create the module file named Packt_Shipme.xml in the folder app/etc/modules with the following content:

    truelocal

  3. Create a config.xml file in the folder app/code/local/Packt/Shipme/etc/ with the following content:

    0.0.1Packt_Shipme_Model1shipme/carrier_shipmeShipme shipping1Express delivery41Business delivery5

  4. Clear the cache and navigate in the backend to System | Configuration | Advanced Disable Modules Output. Observe that the Packt_Shipme module is on the list.

  5. At this point, the module is initialized and working. Now, we have to create a system.xml file where we will put the configuration parameters for our shipping module. Create the file app/code/local/Packt/Shipme/etc/system.xml.
  6. In this file, we will create the configuration parameters for our shipping module. When you paste the following code in the file, you will create an extra group in the shipping method’s configuration. In this group, we can set the settings for the new shipping method:

    15111 selectadminhtml/ system_config_source_yesno10111<label>Title</label> <frontend_type>text</frontend_type><sort_order>20</sort_order><show_in_default>1</show_in_default><show_in_website>1</show_in_website><show_in_store>1</show_in_store>

  7. Clear the cache and navigate in the backend to the shipping method configuration page. To do that, navigate to System | Configuration | Sales | Shipping methods. You will see that an extra group is added as shown in the following screenshot:

  8. You will see that there is a new shipping method called Shipme. We will extend this configuration with some values. Add the following code under the tag of the module:

    selectadminhtml/system_config_source_yesnomodel> 10111

    <label>Title</label> <frontend_type>text</frontend_type><sort_order>20</sort_order><show_in_default>1</show_in_default><show_in_website>1</show_in_website><show_in_store>1</show_in_store> selectadminhtml/system_config_source_yesnomodel> 30111 text40111 text50111 selectadminhtml/system_config_source_yesnomodel> 60111 text70111 text80111
  9. Clear the cache and reload the backend. You will now see the other configurations under the Shipme – Express shipping method as shown in the following screenshot:

How it works…

The first thing we have done is to create the necessary files to initialize the module. The following files are required to initialize a module:

app/etc/modules/Packt_Shipme.xml app/code/local/Packt/Shipme/etc/config.xml

In the first file, we will activate the module with the tag. The tag describes that the module is located in the local code pool, which represents the folder app/code/local/.

In this file, there is also the tag. First this will check if the Mage_Shipping module is installed or not. If not, Magento will throw an exception. If the module is available, the dependency will load this module after the Mage_Shipping module. This makes it possible to rewrite some values from the Mage_Shipping module.

In the second file, config.xml, we configured all the stuff that we will need in this module.

These are the following things:

  • The version number (0.0.1)
  • The models
  • Some default values for the configuration values

The last thing we did was create a system.xml file so that we can create a custom configuration for the shipping module.

The configuration in the system.xml file adds some extra values to the shipping method configuration, which is available in the backend under the menu System | Configuration | Sales | Shipping methods.

In this module, we created a new shipping handler called Shipme. Within this handler, you can configure two shipping options: express and business. In the system.xml file, we created the fields to configure the visibility, name, and price of the options.

See also

In this recipe, we used the system.xml file of the module to create the configuration values.

Writing an adapter model

A new shipping module is initialized in the previous recipe. What we did in the previous recipe was a preparation to continue with the business part we will see in this recipe. We will add a model with the business logic for the shipping method. The model is called an adapter class because Magento requires an adapter class for each shipping method. This class will extend the Mage_Shipping_Model_Carrier_Abstract class.

This class will be used for the following things:

  • Make the shipping method available
  • Calculate the shipping costs
  • Set the title in the frontend of the shipping methods

How to do it…

Perform the following steps to create the adapter class for the shipping method:

  1. Create the folder app/code/local/Packt/Shipme/Model/Carrier if it doesn’t already exist.
  2. In this folder, create a file named Shipme. php and add the following content to it:

    getConfigData('express_enabled')) { $method = Mage::getModel ('shipping/rate_result_method'); $method->setCarrier($this->_code); $method->setCarrierTitle ($this->getConfigData('title')); $method->setMethod('express'); $method->setMethodTitle ($this->getConfigData('express_title')); $method->setCost ($this->getConfigData('express_price')); $method->setPrice ($this->getConfigData('express_price')); $result->append($method); } //Check if business method is enabled if ($this->getConfigData('business_enabled')) { $method = Mage::getModel ('shipping/rate_result_method'); $method->setCarrier($this->_code); $method->setCarrierTitle ($this->getConfigData('title')); $method->setMethod('business'); $method->setMethodTitle ($this->getConfigData('business_title')); $method->setCost ($this->getConfigData('business_price')); $method->setPrice ($this->getConfigData('business_price')); $result->append($method); } return $result; } public function isActive() { $active = $this->getConfigData('active'); return $active==1 || $active=='true'; } public function getAllowedMethods() { return array('shipme'=>$this->getConfigData('name')); } }

  3. Save the file and clear the cache; your adapter model has now created.

How it works…

The previously created class handles all the business logic that is needed for the shipping method. Because this adapter class is an extension of the Mage_Shipping_Model_Carrier_Abstract class, we can overwrite some methods to customize the business logic of the standard.

The first method we overwrite is the isAvailable() function. In this function, we have to return true or false to say that the module is active. In our code, we will activate the module based on the system configuration field active.

The second method is the collectRates() function. This function is used to set the right parameters for every shipping method. For every shipping method, we can set the title and price.

The class implements the interface Mage_Shipping_Model_Carrier_Interface.

In this interface, two functions are declared: the isTrackingAvailable() and getAllowedMethods() functions.

We created the function getAllowedMethods() in the adapter class. The isTrackingAvailable() function is declared in the parent class Mage_Shipping_Model_Carrier_Abstract.

We configured two options under the Shipme shipping method. These options are called Express delivery and Business delivery. We will check if they are enabled in the configuration and set the configured title and price for each option.

The last thing to do is return the right values. We have to return an instance of the class Mage_Shipping_Model_Rate_Result. We created an empty instance of the class, where we will append the methods to when they are available.

To add a method, we have to use the function append($method). This function requires an instance of the class Mage_Shipping_Model_Rate_Result_Method that we created in the two if statements.


Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here