Shipping Modules in Magento: Part 2

0
84
8 min read

Appearing in the administration

Once this has been done, the shipping method should appear in Shipping Methods under System->Configuration:

Magento 1.3: PHP Developer's Guide

Now, we will look at the most useful shipping module fields that are used when putting the shipping module together. These are fields with predefined names and types that have automatically processed the results that they output. Therefore, they require no additional coding in the adaptor module to take them on board; Magento performs these methods straight out of the box.

Free shipping

If we want to enable an automatic price-based amount for free shipping with our method, we can add in a field called free_shipping_enable and combine this with another field by the name of free_shipping_subtotal. When free_shipping_enable is set to Enabled by the Magento administrator, then Magento will automatically take free_shipping_subtotal into account and offer free shipping if the total amount is above the value of free_shipping_subtotal.

If this field is disabled, Magento will simply process using the default shipping calculation behavior of the module.

 

The fields are set up as follows, with sort_order and show_in_ values varying:

<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>

Handling

Handling charges sometimes come into the equation and need to be added onto the overall transaction. Magento enables us to do this using the following source models to present what we want to achieve:

<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>

Restricting a shipping method to certain countries

This will allow us to present the option to the administrator for filtering the shipping method to be only accessible to certain countries. In practice, this means that if we wanted to offer only one type of delivery to the United Kingdom, then we could do so simply by selecting United Kingdom from the multi-select field created by the following declaration.

The Magento administrator can choose the specific countries from the multiple select list. Only orders from those countries that we have created shipping methods for will be processed in the shipping module. This enables them to choose any number of countries for restricting this shipping method to .

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>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>

Using our template to create a shipping method

Now that we have our bare-bones shipping module, we continue with the creation of something that we can see an outcome from. From this we should be able to start to put together our own shipping module tailor-made for future needs.

The purpose of what we are going to build is going to be very simple: we’re going to create a shipping module that meets the following parameters:

  • It has a handling fee, either per product or for the entire order
  • It can be limited to specific countries
  • It can set a simple flat-rate shipping cost, if 10 products or more are being ordered
  • It can set another simple flat-rate shipping cost, if 10 products or less are being ordered
  • All of the above can be configured via the Magento administration

Before progressing, we delete the previous shipping module from our installation to make sure that it does not interfere with what we’ll be building. To do this, we go back to the Magento Downloader  and select Uninstall from the module’s supporting dropdown before committing the changes.

The configuration files

This time, we’ll go with the directory MagentoBook and the name FullShippingModule. For this, our /app/code/local/MagentoBook/ShippingModule/MagentoBook/FullShippingModule/etc/config.xml file will look like:

<?xml version="1.0"?>
<config>
<modules>
<MagentoBook_FullShippingModule>
<version>0.1.0</version>
<depends>
<Mage_Shipping />
</depends>
</MagentoBook_FullShippingModule>
</modules>

<global>
<models>
<FullShippingModule>
<class>MagentoBook_FullShippingModule_Model</class>
</FullShippingModule>
</models>

<resources>
<fullshippingmodule_setup>
<setup>
<module>MagentoBook_FullShippingModule</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</fullshippingmodule_setup>
</resources>
</global>
</config>

We turn on FullShippingModule, and allow it to be turned off/on from within the administration. Then, we create /app/etc/modules/MagentoBook_FullShippingModule.xml and place the following in it:

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

Our adaptor

For those interested in cutting down on code, unnecessary comments have been removed (which were included in the previous adaptor in this article).

We place the following code in: /app/code/local/MagentoBook/FullShippingModule/Model/Carrier/FullBoneMethod.php

<?php
class MagentoBook_FullShippingModule_Model_Carrier_FullBoneMethod
extends Mage_Shipping_Model_Carrier_Abstract
{
protected $_code = 'fullshippingmodule';
public function collectRates(Mage_Shipping_Model_Rate_Request
$request)
{
if (!$this->getConfigData('active')) {
Mage::log('The '.$this->_code.' shipping method is not
active.');
return false;
}
$handling = $this->getConfigData('handling');
$result = Mage::getModel('shipping/rate_result');
$method = Mage::getModel('shipping/rate_result_method');
$items = Mage::getModel('checkout/session')->getQuote()-
>getAllItems();
if (count($items) >= $this->getConfigData('minimum_item_limit')) {
$code = $this->getConfigData('over_minimum_code');
$title = $this->getConfigData('over_minimum_title');
$price = $this->getConfigData('over_minimum_price');
}
else {
$code = $this->getConfigData('under_minimum_code');
$title = $this->getConfigData('under_minimum_title');
$price = $this->getConfigData('under_minimum_price');
}
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($code);
$method->setMethodTitle($title);
$method->setPrice($price + $handling);
$result->append($method);
return $result;
}
}

In short, this will check whether there are more items in the cart than the pre-configured value of minimum_item_limit and then apply a rate if it is over the set limit. If under the limit, it applies another rate.

LEAVE A REPLY

Please enter your comment!
Please enter your name here