magento Remove Shipping Method in Onepage Checkout

magento Remove Shipping Method in Onepage Checkout

In this post we will show you magento Remove Shipping Method in Onepage Checkout, hear for magento Remove Shipping Method in Onepage Checkout we will give you demo and example for implement.

How to Remove Shipping Method Step from One Page Checkout

created a new module with an enable/disable shipping step config setting, so you can always re-enable the shipping step from the system->configuration section.

code for magento Remove Shipping Method in Onepage Checkout

So create the module onlinecode_Checkout.

You will need the following files.

app/etc/modules/onlinecode_Checkout.xml – the declaration file

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

app/code/local/onlinecode/Checkout/etc/config.xml – the configuration file where you define models, blocks and rewrite the onepage checkout block. Also it sets a default shipping method.

<?xml version="1.0"?>
<config>
    <modules>
        <onlinecode_Checkout>
            <version>0.0.1</version>
        </onlinecode_Checkout>
    </modules>
    <global>
        <blocks>
            <checkout>
                <rewrite>
                    <onepage>onlinecode_Checkout_Block_Onepage</onepage><!-- rewrite the onepage chackout block -->
                </rewrite>
            </checkout>
        </blocks>
        <helpers>
            <onlinecode_checkout>
                <class>onlinecode_Checkout_Helper</class>
            </onlinecode_checkout>
        </helpers>
        <models>
            <onlinecode_checkout>
                <class>onlinecode_Checkout_Model</class>
            </onlinecode_checkout>
        </models>
    </global>
    <default>
        <checkout>
            <options>
                <hide_shipping>1</hide_shipping>
                <default_shipping>tablerate_bestway</default_shipping><!-- set the default shipping method code -->
            </options>
        </checkout>
    </default>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <onlinecode_Checkout before="Mage_Checkout">onlinecode_Checkout</onlinecode_Checkout>
                    </modules>
                </args>
            </checkout>
        </routers>
        <translate>
            <modules>
                <onlinecode_Checkout>
                    <files>
                        <default>onlinecode_Checkout.csv</default>
                    </files>
                </onlinecode_Checkout>
            </modules>
        </translate>
    </frontend>
</config>

app/code/local/onlinecode/Checkout/etc/system.xml – the system file that places the enabled/disabled flag for the shipping step

<?xml version="1.0"?>
<config>
    <sections>
        <checkout>
            <groups>
                <options>
                    <fields>
                        <hide_shipping translate="label" module="onlinecode_checkout">
                            <label>Hide shipping method step</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>100</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </hide_shipping>
                        <default_shipping translate="label" module="onlinecode_checkout">
                            <label>Default shipping method code</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>110</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </default_shipping>
                    </fields>
                </options>
            </groups>
        </checkout>
    </sections>
</config>

app/code/local/onlinecode/Checkout/Helper/Data.php – the helper that checks if the shipping step should be disabled

<?php
class onlinecode_Checkout_Helper_Data extends Mage_Core_Helper_Abstract 
{
    const XML_HIDE_SHIPPING_PATH = 'checkout/options/hide_shipping';
    const XML_DEFAULT_SHIPPING_PATH = 'checkout/options/default_shipping';
    public function getHideShipping()
    {
        if (!Mage::getStoreConfigFlag(self::XML_HIDE_SHIPPING_PATH)){
            return false;
        }
        if (!$this->getDefaultShippingMethod()){
            return false;
        }
        return true;
    }
    public function getDefaultShippingMethod()
    {
        return Mage::getStoreConfig(self::XML_DEFAULT_SHIPPING_PATH);
    }
}

app/code/local/onlinecode/Checkout/Block/Onepage.php – the overwritten checkout block

<?php
class onlinecode_Checkout_Block_Onepage extends Mage_Checkout_Block_Onepage 
{
    protected function _getStepCodes()
    {
        if (!Mage::helper('onlinecode_checkout')->getHideShipping()){
            return parent::_getStepCodes();
        }
        return array_diff(parent::_getStepCodes(), array('shipping_method'));
    }
}

app/code/local/onlinecode/Checkout/controllers/OnepageController.php – override the onepage controller to set automatically the default shipping method.

<?php
require 'Mage/Checkout/controllers/OnepageController.php';
class onlinecode_Checkout_OnepageController extends Mage_Checkout_OnepageController
{
    public function saveBillingAction()
    {
        if (!Mage::helper('onlinecode_checkout')->getHideShipping()){
            parent::saveBillingAction();
            return;
        }

        if ($this->_expireAjax()) {
            return;
        }
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('billing', array());
            $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);

            if (isset($data['email'])) {
                $data['email'] = trim($data['email']);
            }
            $result = $this->getOnepage()->saveBilling($data, $customerAddressId);

            if (!isset($result['error'])) {
                /* check quote for virtual */
                if ($this->getOnepage()->getQuote()->isVirtual()) {
                    $result['goto_section'] = 'payment';
                    $result['update_section'] = array(
                        'name' => 'payment-method',
                        'html' => $this->_getPaymentMethodsHtml()
                    );
                } elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                    //add default shipping method
                    $data = Mage::helper('onlinecode_checkout')->getDefaultShippingMethod();
                    $result = $this->getOnepage()->saveShippingMethod($data);
                    $this->getOnepage()->getQuote()->save();
                    /*
                    $result will have erro data if shipping method is empty
                    */
                    if(!$result) {
                        Mage::dispatchEvent('checkout_controller_onepage_save_shipping_method',
                            array('request'=>$this->getRequest(),
                                'quote'=>$this->getOnepage()->getQuote()));
                        $this->getOnepage()->getQuote()->collectTotals();
                        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

                        $result['goto_section'] = 'payment';
                        $result['update_section'] = array(
                            'name' => 'payment-method',
                            'html' => $this->_getPaymentMethodsHtml()
                        );
                    }


                    $result['allow_sections'] = array('shipping');
                    $result['duplicateBillingInfo'] = 'true';
                } else {
                    $result['goto_section'] = 'shipping';
                }
            }

            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        }
    }
    public function saveShippingAction()
    {
        if (!Mage::helper('onlinecode_checkout')->getHideShipping()){
            parent::saveShippingAction();
            return;
        }
        if ($this->_expireAjax()) {
            return;
        }
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('shipping', array());
            $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
            $result = $this->getOnepage()->saveShipping($data, $customerAddressId);

            $data = Mage::helper('onlinecode_checkout')->getDefaultShippingMethod();
            $result = $this->getOnepage()->saveShippingMethod($data);
            $this->getOnepage()->getQuote()->save();

            if (!isset($result['error'])) {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
                );
            }
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        }
    }
}

Clear the cache and you are done.

Hope this code and post will helped you for implement magento Remove Shipping Method in Onepage Checkout. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  47  =  50

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US