Category: Magento Certification

Creating custom shipping method in magento

This tutorial is dedicated to Magento Certification. Let’s start.

NameSpace : MyPackage and Module: CustomShipping

Register the module

<!-- app/etc/modules/MyPackage_CustomShipping.xml -->
<?xml version="1.0"?>
<config>
    <modules>
        <MyPackage_CustomShipping>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Shipping />
            </depends>
        </MyPackage_CustomShipping>
    </modules>
</config>


Create the config

<!-- app/code/local/MyPackage/CustomShipping/etc/config.xml -->
<?xml version="1.0"?>
<config>
    <modules>
        <MyPackage_CustomShipping>
            <version>0.0.0.1</version>
        </MyPackage_CustomShipping>
    </modules>
    <global>
        <models>
            <customshipping>
                <class>MyPackage_CustomShipping_Model</class>
            </customshipping>
        </models>
    </global>
    <!-- Default configuration -->
    <default>
        <carriers>
            <customshipping>
                <active>0</active>
                <title>Custom Shipping</title>
                <name>Standard</name>
                <price>9</price>
                <!-- this model hold all logic of custom module -->
                <model>customshipping/carrier_customshipping</model>
                <sallowspecific>0</sallowspecific>
                <sort_order>0</sort_order>
            </customshipping>
        </carriers>
    </default>
</config>

Adapter model
To create our shipping carrier, we need to extend Mage_Shipping_Model_Carrier_Abstract, implement Mage_Shipping_Model_Carrier_Interface and add the required abstract methods.

The most important method is collectRates and getAllowedMethods. collectRates is the method that receives a shipping request, appends applicable shipping methods and returns a shipping result.

<?php
//app/code/local/MyPackage/CustomShipping/Model/Carrier/Customshipping.php
class MyPackage_CustomShipping_Model_Carrier_Customshipping
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{
    protected $_code = 'customshipping';

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {

        return Mage::getModel('shipping/rate_result');
    }

    public function getAllowedMethods()
    {
        return array('custom_shipping'=>$this->getConfigData('name'));
    }
}

This is the skeleton for a shipping method class, it has no meaning if it has no shipping method.

Now creating meaningful of this method, suppose we use configure price from admin. Default method “Standard” and price is “9”.

<?php
//app/code/local/MyPackage/CustomShipping/Model/Carrier/Customshipping.php
class MyPackage_CustomShipping_Model_Carrier_Customshipping
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{
    protected $_code = 'customshipping';

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $result = Mage::getModel('shipping/rate_result');
        $method = Mage::getModel('shipping/rate_result_method');

        $method->setCarrier('customshipping');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('custom_shipping');
        $method->setMethodTitle($this->getConfigData('name'));

        $method->setPrice($this->getConfigData('price'));
        $method->setCost($this->getConfigData('price'));

        $result->append($method);

        return $result;
    }

    public function getAllowedMethods()
    {
        return array('custom_shipping'=>$this->getConfigData('name'));
    }
}

System Configuration

<!-- app/code/local/MyPackage/CustomShipping/etc/system.xml -->
<?xml version="1.0"?>
<config>
    <sections>
        <carriers>
            <groups>
                <customshipping translate="label">
                    <label>Custom Configurable Shipping</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>
                    <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>0</show_in_store>
                        </active>
                        <name translate="label">
                            <label>Method Name</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>1</show_in_store>
                        </name>
                        <price translate="label">
                            <label>Fixed Price</label>
                            <frontend_type>text</frontend_type>
                            <validate>validate-number validate-zero-or-greater</validate>
                            <sort_order>5</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </price>
                        <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>0</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>
                    </fields>
                </customshipping>
            </groups>
        </carriers>
    </sections>
</config>

This is simple shipping method module creation.
N.B: Inside shipping method model class, you should take care of following condition

1. $item->getHasChildren() && $item->isShipSeparately()
2. $item->getProduct()->isVirtual() == true
3. $item->getProduct()->isVirtual() || $item->getParentItem()
4. $item->getFreeShipping() == true

Happy Coding!

Magento Certified Developer – Rendering (part 1)

From today I will start a series of Magento Certified Developer exam preparation, which completely follow the Certification Study Guide. So stay with me 🙂 . I randomly pick a topic of Magento Rendering process, This is interesting to me. So start now.

Topic : 3- Rendering
From this topic approximately 7% of question available in exam.

Themes in Magento
Themes inside of a design package contain the actual files that determine the visual output and frontend functionality of your store. Magento themes contain templating information (layout files, template files, theme-specific translation files) and skinning information (CSS files, images, and theme-specific JavaScript files). A theme can belongs to only one design package. Themes are locate

app/design/area/package/theme_name

area: frontend/adminhtml/install
package: base(by default)/default(by default)/custom_package
theme_name: default/ custom_theme

So when you creating a new theme,it’s really good to create new package with new theme. Don’t change base/core file.. It’s really horrible for future update.

Register Custom Theme
Two ways you can register custom theme. That is
1. If you want your theme to be used ‘always’ as your store theme, you may tell Magento to use it from System => Configuration => Design
2. Another way to register theme form going System => Design, available this theme for all time or for certain period of time.

What is the difference between package and theme
Package : A design package is a collection of related themes.
Theme : A theme is a collection of template,layout file and js,css,images that’s create the visual experience of your store.

Theme must have one package and package has many theme with default one.

What happens if the requested file is missed in your theme/package
If a particular file is missed in a theme, there is are some fallback themes and packages where Magento will try to find it.
1. First location in the fallback chain is the default theme of the current package.
2. Second location is the base package default theme which exists in a stock Magento install.

You can also define a third point in the middle of these by defining a custom default theme (with a different name than ‘default’) in your admin panel.

This enough for today. Next article I will discuss vastly. Good luck and Thank you!