Question:

Custom Shipping Module not work on frontend in Magento v1.9

Addison: 2 weeks ago

I had created a custom shipping module. But when I enable it and check on the frontend for my custom shipping rate, then I add billing detail and click on the Continue button, it redirects me to the cart page.

Here is the code of my custom shipping module.

app/etc/modules/Custom_ShippingMethod.xml

<config>
    <modules>
        <Custom_ShippingMethod>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Shipping />
            </depends>
        </Custom_ShippingMethod>
    </modules>
</config>

app/code/local/Custom/ShippingMethod/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_ShippingMethod>
            <version>1.0</version>
        </Custom_ShippingMethod>
    </modules>
  <global>
        <models>
            <customshippingmethod>
                <class>Custom_ShippingMethod_Model</class>
            </customshippingmethod>
        </models>
        <helpers>
          <customshippingmethod>
            <class>Custom_ShippingMethod_Helper</class>
          </customshippingmethod>
        </helpers>
    </global>
    <default>
        <carriers>
          <customshippingmethod>
               <active>1</active>
               <model>shipping/carrier_customshippingmethod</model>
               <title>Custom Shipping Carrier</title>
               <name>My Custom Shipping Method</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>
            </customshippingmethod>
         </carriers>
       </default>
</config>

app/code/local/Custom/ShippingMethod/etc/system.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <carriers>
            <groups>
                <customshippingmethod translate="label" module="shipping">
                    <label>Custom Shipping</label>
                    <frontend_type>text</frontend_type>
                    <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>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>
                </customshippingmethod>
            </groups>
        </carriers>
    </sections>
</config>

app/code/local/Custom/ShippingMethod/Model/Customshipping.php

<?php
class Custom_ShippingMethod_Model_Customshipping extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{

    protected $_code = '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(
            '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;
    }
}

app/code/local/Custom/ShippingMethod/Helper/Customshipping.php

<?php
class Custom_ShippingMethod_Helper_Customshipping extends Mage_Core_Helper_Abstract
{

}

I got my custom shipping module configuration like this in admin. Please check the attached screenshot. enter image description here

But on the frontend when I add the product to cart and go to checkout, after filliing up the billing info and click on "Continue" for Shipping Method. Then I am getting redirected on the cart page.

Can anybody suggest me where I am wrong. I had reffered this (http://excellencemagentoblog.com/blog/2011/10/14/magento-create-custom-shipping-method/) article.

Answer:
Leo: 2 weeks ago

I checked your code and found following error at:

app/code/local/Custom/ShippingMethod/etc/config.xml

  1. You need change model in <customshippingmethod> tag from <model>shipping/carrier_customshippingmethod</model> to <model>shippingmethod/customshipping</model>

Explanation: model name and path is wrong and it should be [modulename]/[path_to_model]

  1. helper and model class should be in module tag i.e <shippingmethod> not <customshippingmethod>

I am attaching diff of file

enter image description here

Correct file should be:

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_ShippingMethod>
            <version>1.0</version>
        </Custom_ShippingMethod>
    </modules>
  <global>
        <models>
            <shippingmethod>
                <class>Custom_ShippingMethod_Model</class>
            </shippingmethod>
        </models>
        <helpers>
          <shippingmethod>
            <class>Custom_ShippingMethod_Helper</class>
          </shippingmethod>
        </helpers>
    </global>
    <default>
        <carriers>
          <customshippingmethod>
               <active>1</active>
               <model>shippingmethod/customshipping</model>
               <title>Custom Shipping Carrier</title>
               <name>My Custom Shipping Method</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>
            </customshippingmethod>
         </carriers>
       </default>
</config>

Hope above will Help!