Disable shipping method on the frontend with a few tweaks

Page 1

Disable Shipping Method on the Frontend with a Few Tweaks Magento, in its default state, does not support the functionality wherein you can disable shipping method from the frontend, but leave it enabled in the admin area of your Magento website. Well, while it is not functionally supported by Magento, you can always add some tweaks to make it possible. Configurations for the different functionalities are available at global, website and store levels for Magento, but at a global/website level, you can either enable/disable the shipping methods. At the store level, you can only edit the title of your store. You will not be able to enable or disable shipping at store level.

Let’s take a simple case to understand the tweak better. Scenario 1: you have a single website within your Magento system. Go to sales>order>generate new order. You will see the “please select a customer grid” option blinking on your screen from where you will need to choose the customer for whom you want to create the order. Scenario 2: You have two websites defined in your Magento system Go to sales>order>generate new order. Now, you will see “please select a website”. Once you have selected the website, the following line will appear “please select a customer”. Now, that you have seen two different scenarios, let’s try to understand what’s really happening. Either you use the default website (as you did when working with single website), or choose the website you want to create the customer for. In both scenarios, after reading the website configuration, the shipping data is retrieved from the website. This simply means that you cannot configure the store to show the shipping method at the admin side only.


How can we overcome this issue, and configure the shipping method to be shown only in the admin area? Simple! You will need to rewrite Mage_Shipping_Model_Config and Mage_Sales_Model_Quote_Address Go to app/code/local/Mycompany/Myextension/etc/config.xml <models> <shipping> <rewrite> <config>Mycompany_Myextension_Model_Shipping_Config</config> </rewrite> </shipping> <sales> <rewrite> <quote_address>Mycompany_Myextension_Model_Sales_Quote_Address</quote_address> </rewrite> </sales> </models> Configuring the shipping methods Go to app/code/local/Mycompany/Myextension/Model/Adminhtml/System/Config/Source/Shipping/M ethods

<?php

class Mycompany_Myextension_Model_Adminhtml_System_Config_Source_Shipping_Methods { protected $_options;


public function toOptionArray() { $carriers = Mage::getSingleton('shipping/config')->getAllCarriers();

$carriersActive = Mage::getSingleton('shipping/config')->getActiveCarriers(); $carriersActive = array_keys($carriersActive);

if (!$this->_options) { foreach ($carriers as $carrier) { $carrierCode = $carrier->getId(); $carrierTitle = Mage::getStoreConfig('carriers/'.$carrierCode.'/title', Mage::app()>getStore()->getId()); $carrierTitle = trim($carrierTitle);

if (empty($carrierTitle)) { continue; }

if (in_array($carrierCode, $carriersActive)) { $carrierTitle = sprintf('%s (currently active)', $carrierTitle); } else { $carrierTitle = sprintf('%s (currently inactive)', $carrierTitle); }

$this->_options[] = array('value'=>$carrierCode, 'label'=>$carrierTitle); }


}

$options = $this->_options;

array_unshift($options, array('value'=>'', 'label'=> ''));

return $options; } } The system.xml defines the file “source_model� Go to app/code/local/Mycompany/Myextension/etc/system.xml <fields> <frontend_hidden_methods> <label>Hide Shipping Methods on Frontend</label> <comment><![CDATA[If a shipping method has been enabled under its settings, you can choose to hide it on the frontend by selecting it here from the list. This way, the shipping method would be available from the admin area but not from the frontend.]]></comment> <frontend_type>multiselect</frontend_type> <source_model>mycompany_myextension/adminhtml_system_config_source_shipping_methods </source_model> <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> </frontend_hidden_methods> </fields> The actual config definition has been shown partially as part of the above code


Defining the helper data Go to app/code/local/Mycompany/Myextension/Helper/Data.php. Paste the following code here <?php class Mycompany_Myextension_Helper_Data extends Mage_Core_Helper_Abstract { const CONFIG_PATH_HIDE_FRONTEND_SHIPPING_METHODS = 'some-path-to-system-xml-field';

public function getHiddenFrontendShippingMethods() { $methods = Mage::getStoreConfig(self::CONFIG_PATH_HIDE_FRONTEND_SHIPPING_METHODS); $methods = explode(',', $methods); return $methods; } } Rewrite Shipping Config Go to app/code/local/Mycompany/Myextension/Model/Shipping/Config.php <?php class Mycompany_Myextension_Model_Shipping_Config extends Mage_Shipping_Model_Config { public function getActiveCarriers($store = null) { $carriers = parent::getActiveCarriers($store); if (Mage::getDesign()->getArea() === Mage_Core_Model_App_Area::AREA_FRONTEND) { $carriersCodes = array_keys($carriers); $hiddenShippingMethods = Mage::helper('mycompany_myextension')>getHiddenFrontendShippingMethods();


foreach ($carriersCodes as $carriersCode) { if (in_array($carriersCode, $hiddenShippingMethods)) { unset($carriers[$carriersCode]); } } } return $carriers; } } Rewrite quote address. Go to app/code/local/Mycompany/Myextension/Model/Sales/Quote/Address.php <?php class Mycompany_Myextension_Model_Sales_Quote_Address Mage_Sales_Model_Quote_Address

extends

{ public function getShippingRatesCollection() { parent::getShippingRatesCollection(); $hiddenFrontendShippingMethods = Mage::helper('mycompany_myextension')>getHiddenFrontendShippingMethods(); $removeRates = array(); foreach ($this->_rates as $key => $rate) { if (in_array($rate->getCarrier(), $hiddenFrontendShippingMethods)) { $removeRates[] = $key; } }


foreach ($removeRates as $key) { $this->_rates->removeItemByKey($key); } return $this->_rates; } } With these rewrites, you have created a select option using which you can click on methods that you want to display at the front end, and the ones that you want to enable at the admin area. Do not confuse this hide from frontend method with enable/disable for shipping methods. Conclusion With this method, you can hide certain shipping methods from the front end. With this method in use, you can customize shipping methods such that you can display certain methods on the frontend, while enable certain others only in the backend. Author Bio: Deepa is writer who loves writing blog on Magento Topic. In case if you are looking for Magento Development Services and Magento Development in UK you may check for getting the same at https://www.silvertouchtech.co.uk.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.