cancel
Showing results for 
Search instead for 
Did you mean: 

Disable Shipping Calculation AJAX

Disable Shipping Calculation AJAX

Hello,

Is there a way to disable the dynamic shipping calculation that happens every time a customer changes part of their address?  We are getting charged for many extraneous hits to our shipping calculation API and are looking to reduce these.  Thank you.

1 REPLY 1

Re: Disable Shipping Calculation AJAX

This Can be achieved by creating custom shipping method and then returning False as Follows :
Make Custom Module : Vendor_ModuleName
and add the below files :

app/code/Vendor/ModuleName/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <!--Custom shipping Methods-->
        <section id="carriers" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <group id="custom_shipping" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Custom Shipping Method</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                </field>
            </group>
        </section>
    </system>
</config>

Now add file : app/code/Vendor/ModuleName/etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <carriers>
            <custom_shipping>
                <active>0</active>
                <model>Vendor\ModuleName\Model\Carrier\Shipping</model>
                <name>Custom Shipping</name>
                <title>Custom Shipping</title>
            </custom_shipping>
        </carriers>
    </default>
</config>

Finally Add this File : app/code/Vendor/ModuleName/Model/Carrier/Shipping.php

<?php
namespace Vendor\ModuleName\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;

class Shipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
    \Magento\Shipping\Model\Carrier\CarrierInterface
{
    const SHIPPING_METHOD_CODE = "custom_shipping";
    /**
     * @var string
     */
    public $_code = self::SHIPPING_METHOD_CODE;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    public $_logger;

    /**
     * @var bool
     */
    public $_isFixed = true;

    /**
     * @var \Magento\Shipping\Model\Rate\ResultFactory
     */
    public $_rateResultFactory;
    /**
     * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
     */
    public $_rateMethodFactory;
    /**
     * @var \Magento\Checkout\Model\Cart
     */
    public $cart;
    /**
     * @var \Magento\Framework\App\State
     */
    public $appState;

    /**
     * Shipping constructor.
     * @param \Magento\Framework\App\State $appState
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
     * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
     * @param \Magento\Checkout\Model\Cart $cart
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\App\State $appState,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
        \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
        \Magento\Checkout\Model\Cart $cart,
        array $data = []
    ) {
        $this->appState = $appState;
        $this->_rateResultFactory = $rateResultFactory;
        $this->_rateMethodFactory = $rateMethodFactory;
        $this->_logger = $logger;
        $this->cart = $cart;
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
    }

    /**
     * @param RateRequest $request
     * @return bool
     */
    public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
        $countryCode = $request->getDestCountryId(); // Getting Country Code From request
        /**
         * @var \Magento\Shipping\Model\Rate\Result $result
         */
        //$result = $this->_rateResultFactory->create();
        return false; // Here It will return false
    }
    /**
     * @return array
     */
    public function getAllowedMethods()
    {
        return [$this->_code => $this->getConfigData('title')];
    }
    /**
     * @return string
     */
    public function getCode()
    {
        return $this->_code;
    }
}


Problem Solved !!
Click on 'Kudos' & Accept as Solution to encourage to write more answers !