Shipping method in magento

How to create shipping method in magento?

In this extension we created shipping according to “number of quantity product” or according to “number of order”.

Note:- you can download extension from this rar_image
Add shipping method in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab and which is in System tab.
there are many steps to create this extension.
Step1:- Create Mywork_MyShipping.xml file in app/etc/modules/.
where Mywork is Namespace and MyShipping is Modulename.

<?xml version="1.0"?>
<config>
    <modules>
        <Mywork_MyShipping>
            <active>true</active>
            <codePool>local</codePool>
	       <depends>
			 <Mage_Shipping />
	       </depends>
        </Mywork_MyShipping>        
    </modules>
</config>

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/MyShipping/etc/

<?xml version="1.0"?>
<config>
<modules>
        <Mywork_MyShipping>
            <version>1.0.0</version>
        </Mywork_MyShipping>
</modules>

     <global>
	 <helpers>
            <myshipping>
                <class>Mywork_MyShipping_Helper</class>
            </myshipping>
    </helpers>
	
	<models>
            <mywork_myshipping>
                <class>Mywork_MyShipping_Model</class>
            </mywork_myshipping>
    </models>
	</global>
	
	<default>
    <carriers>
      <mywork_myshipping>
        <active>1</active>
        <sallowspecific>1</sallowspecific>
        <model>mywork_myshipping/myshipping</model>
        <name>Total Fixed Rs.</name>
        <price>30.00</price>
        <title>My Shipping</title>
		<type>I</type>
      </mywork_myshipping>
    </carriers>
  </default>	
</config>

Step3:- Now create the system.xml file in app/code/local/Mywork/MyShipping/etc/

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <sections>
    <carriers>
      <groups>
        <mywork_myshipping translate="label">
          <label>My 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>1</show_in_website>
              <show_in_store>0</show_in_store>
            </active>
            <title translate="label">
              <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>
            </title>
			
	    <price translate="label">
               <label>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>
		   
          <type translate="label">
               <label>Type</label>
	       <frontend_type>select</frontend_type>
		<source_model>Mywork_MyShipping_Model_Myshippingoption</source_model>
		<sort_order>4</sort_order>
		<show_in_default>1</show_in_default>
		<show_in_website>1</show_in_website>
		<show_in_store>0</show_in_store>
          </type>
			
	  <sallowspecific translate="label">
              <label>Ship to Applicable Countries</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>
          </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>
              <can_be_empty>1</can_be_empty>
            </specificcountry>
			
          </fields>
        </mywork_myshipping>
      </groups>
    </carriers>
  </sections>
</config>

Step4:- Now create the Myshipping.php file in app/code/local/Mywork/MyShipping/Model/

<?php
class Mywork_MyShipping_Model_Myshipping extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
  protected $_code = 'mywork_myshipping';
  protected $_isFixed = true;

    /**
     * Enter description here...
     *
     * @param Mage_Shipping_Model_Rate_Request $data
     * @return Mage_Shipping_Model_Rate_Result
     */
    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

        $freeBoxes = 0;
        if ($request->getAllItems()) {
            foreach ($request->getAllItems() as $item) {

                if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                    continue;
                }

                if ($item->getHasChildren() && $item->isShipSeparately()) {
                    foreach ($item->getChildren() as $child) {
                        if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                            $freeBoxes += $item->getQty() * $child->getQty();
                        }
                    }
                } elseif ($item->getFreeShipping()) {
                    $freeBoxes += $item->getQty();
                }
            }
        }
        $this->setFreeBoxes($freeBoxes);

        $result = Mage::getModel('shipping/rate_result');
        if ($this->getConfigData('type') == 'O') { // per order
            $shippingPrice = $this->getConfigData('price');
        } elseif ($this->getConfigData('type') == 'I') { // per item
            $shippingPrice = ($request->getPackageQty() * $this->getConfigData('price')) - ($this->getFreeBoxes() * $this->getConfigData('price'));
        } else {
            $shippingPrice = false;
        }

        $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);

        if ($shippingPrice !== false) {
            $method = Mage::getModel('shipping/rate_result_method');

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

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

            if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
                $shippingPrice = '0.00';
            }


            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);

            $result->append($method);
        }

        return $result;
    }

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

Step5:- Now create the Myshippingoption.php file in app/code/local/Mywork/MyShipping/Model/

<?php
class Mywork_MyShipping_Model_Myshippingoption
{
	public function toOptionArray()
    {
        return array(
            array('value'=>'', 'label'=> Mage::helper('adminhtml')->__('None')),
            array('value'=>'O', 'label'=>Mage::helper('adminhtml')->__('Per Order')),
            array('value'=>'I', 'label'=>Mage::helper('adminhtml')->__('Per Item')),
        );
    }
}

Note:- On backend you can change shipping price according to you. click on admin section system->configuration->Sales->Shipping Methods.
shipping_method_backend

Note:- On frontend you can choose shipping method.
shipping_method_frontend