Question

I'm using the table rate shipping method and providing a Free shipping method above 99.

So if the subtotal is below 99 then I need to display the Free shipping method but don't allow it to select by the customer.

Please give me some suggestions.

No correct solution

OTHER TIPS

Isn't it better to not display the method at all if the customer does not meet the necessary conditions?

If so here is how you can achieve this (let me know you need any further assistance):

On your app/code/vendor/module/etc/config.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Store/etc/config.xsd">
    <default>
        <carriers>
            <freeshippingmethod>
                <active>1</active>
                <title translate="true">Free Shipping.</title>
                
                <sallowspecific>0</sallowspecific>
                <sort_order>14</sort_order>
                <model>vendor\module\Model\Carrier\FreeShippingCustomMethod</model>
            </freeshippingmethod>
        </carriers>
    </default>
</config>

On app/code/Model/Carrier/FreeShippingCustomMethod.php

<?php

namespace vendor\module\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;

class FreeShippingCustomMethod extends AbstractCarrier implements CarrierInterface
{

    const FREESHIPPING_CODE       = 'freeshippingmethod';
    const FREESHIPPING_COMPOSTO   = 'freeshippingmethod_freeshippingmethod';
    /**
     * @var string
     */
    protected $_code = self::FREESHIPPING_CODE;

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

    /**
     * @var \Magento\Shipping\Model\Rate\ResultFactory
     */
    private $rateResultFactory;

    /**
     * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
     */
    private $rateMethodFactory;

     /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $storeManager;

     /**
     * @var \Magento\Checkout\Model\Session\Proxy
     */
    private $checkoutSession;

     /**
     * @var \Magento\Catalog\Model\ProductRepository
     */
    private $productRepository;
    
    /**
     * @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 array $data
     */
    public function __construct(
        \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\Store\Model\StoreManagerInterface                  $storeManager,
        \Magento\Checkout\Model\Session\Proxy                       $checkoutSession,
        \Magento\Catalog\Model\ProductRepository                    $productRepository,
        \Magento\Catalog\Api\CategoryRepositoryInterface            $categoryRepository,
        \Magento\Customer\Api\CustomerRepositoryInterface           $customerRepositoryInterface,
        \Magento\Customer\Model\Session                             $customerSession,
        \Magento\Catalog\Model\ProductFactory                       $productloader,  
        array $data = []
        
    ) {
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);

        $this->rateResultFactory           = $rateResultFactory;
        $this->rateMethodFactory           = $rateMethodFactory;
        $this->storeManager                = $storeManager;
        $this->checkoutSession             = $checkoutSession;
        $this->productRepository           = $productRepository;
        $this->categoryRepository          = $categoryRepository;
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->customerSession = $customerSession;
        $this->productloader   = $productloader;
    }

    /**
     * Custom Shipping Rates Collector
     *
     * @param RateRequest $request
     * @return \Magento\Shipping\Model\Rate\Result|bool
     */
    public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
        
        $result = $this->rateResultFactory->create();
        $quote  = $this->checkoutSession->getQuote();

        if( isset( $quote))
        {
            
                    $flagHasFreeShipiing = Do your check here !!!
                    if( $flagHasFreeShipiing != true)
                    {
                        return false;
                    }

                    $method = $this->rateMethodFactory->create();
                    $method->setCarrier(        $this->_code);
                    $method->setCarrierTitle(   $this->getConfigData('title'));
                    $method->setMethod(         $this->_code);
                    $method->setMethodTitle(    $this->getConfigData('name'));
                    

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

                    $result->append(        $method);

                    return $result;
                }
            }
            else
            {
                return false;
            }
        }
    }

    /**
     * @return array
     */
    public function getAllowedMethods()
    {
        return [$this->_code => $this->getConfigData('name')];
    }    
}

?>

If you want this logic then you have to add disabled radio button based on condition for that add first disabled and display:none radio in below file.

/app/design/frontend/Your/Theme/Magento_Checkout/web/template/cart/shipping-rates.html

and you have to add function in

/app/design/frontend/Your/Theme/Magento_Checkout/web/js/view/cart/shipping-rates.js

in this function you have to add ajax controller which is used for show hide that radio button based on condition.

its quite difficult if you have no longer worked with knockout.

mark as solution if its a right answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top