Hello Readers,
In this tutorial, i am going to explain How to add a new Custom Shipping Method to existing Magento Installation which will accept value (i.e shipping charges) from System->configuration option. Here i have used Custom as a module name. You can create your Own module name & change class names accordingly.
Lets start by creating the following folders:
app/code/local/Custom/Customshippingmethod/etc
app/code/local/Custom/Customshippingmethod/controllersr
app/code/local/Custom/Customshippingmethod/Model
1. create the system.xml (under etc folder) file for admin entries.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<sections>
<carriers>
<groups>
<custom_customshippingmethod translate="label">
<label>Custom Demo Shipping Method</label>
<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>0</show_in_website>
<show_in_store>1</show_in_store>
</active>
<title translate="label">
<label>Shipping Method Name</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<show_in_store>1</show_in_store>
</title>
<sallowspecific translate="label">
<label>For selected countries only</label>
<frontend_type>select</frontend_type>
<frontend_class>shipping-applicable-country</frontend_class>
<source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<show_in_store>1</show_in_store>
<show_in_store>2</show_in_store>
</sallowspecific>
<specificcountry translate="label">
<label>Ship to Specific Countries</label>
<frontend_type>multiselect</frontend_type>
<sort_order>31</sort_order>
<source_model>adminhtml/system_config_source_country</source_model>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<show_in_store>1</show_in_store>
<show_in_store>2</show_in_store>
<can_be_empty>1</can_be_empty>
</specificcountry>
<price translate="label">
<label>Price</label>
<frontend_type>text</frontend_type>
<sort_order>32</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<show_in_store>1</show_in_store>
</price>
</fields>
</custom_customshippingmethod>
</groups>
</carriers>
</sections>
</config>
Here active, title, sallowspecific and specificcountry tags are automatically handled by Magento
2. create a file config.xml (under etc folder) of custom module i.e (Customshippingmethod) and add the below code:
<?xml version="1.0"?>
<config>
<modules>
<Custom_Customshippingmethod>
<version>1.0</version>
</Custom_Customshippingmethod>
</modules>
<global>
<models>
<custom_customshippingmethod>
<class>custom_Customshippingmethod_Model</class>
</custom_customshippingmethod>
</models>
</global>
<default>
<carriers>
<custom_customshippingmethod>
<active>1</active>
<sallowspecific>1</sallowspecific>
<model>custom_customshippingmethod/demo</model>
<name>Demo Shipping Method One</name>
<price>10.00</price>
<title>Demo Shipping Method</title>
</custom_customshippingmethod>
</carriers>
</default>
</config>
<default> tag is used to assign default values to our system.xml fields( Here we specify default values for active,title,name,price etc.)
<model> tag contains the path of our shipping model.
3. Create shipping model class. The model class name is Demo.php and is created inside Model folder.
<?php
class Custom_Customshippingmethod_Model_Demo
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = 'custom_customshippingmethod';
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
$result = Mage::getModel('shipping/rate_result');
$result->append($this->_getDefaultRate());
return $result;
}
public function getAllowedMethods()
{
return array(
'custom_customshippingmethod' => $this->getConfigData('name'),
);
}
protected function _getDefaultRate()
{
$rate = Mage::getModel('shipping/rate_result_method');
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
$rate->setMethod($this->_code);
$rate->setMethodTitle($this->getConfigData('name'));
$rate->setPrice($this->getConfigData('price'));
$rate->setCost(0);
return $rate;
}
}
This module has been tested for community edition 1.9.2.4 and you can download module zip file here.
0 Comment(s)