3

Is there any way to done a backend selected shipping method only display in frontend (checkout)?

Actually expected as

Backend:

I need to show dynamically selected shipping method alone shown in frontend checkout:

backend setting example

Frontend:

actual result on frontend

  • Please make Enable status only for table rate shipping method for your front-end website/store – Rajesh Hothi Nov 6 '19 at 13:23
  • not only needed for "Table rate" shipping method and i needed for dynamically selected shipping method shown in frontend – divya sekar Nov 6 '19 at 13:29
  • Is that your custom field...? – Ranganathan Nov 6 '19 at 13:31
  • yes specific shipping method is custom field – divya sekar Nov 6 '19 at 13:50
2

Let suppose you know which method is available (using it's code). You need a one plugin:

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Plugins -->
    <type name="Magento\Shipping\Model\Rate\Result">
        <plugin name="disable_shipping_rates_in_some_case"
                type="Vendor\Module\Model\Plugin\Shipping\Rate\Result\GetAllRates"
                sortOrder="100"
                disabled="false" />
    </type>
</config>

app/code/Vendor/Module/Model/Plugin/Shipping/Rate/Result/GetAllRates.php

<?php

namespace Vendor\Module\Model\Plugin\Shipping\Rate\Result;

/**
 * Class GetAllRates
 */
class GetAllRates
{
    /**
     * @param $subject
     * @param $result
     * @return \Magento\Quote\Model\Quote\Address\RateResult\Method[]
     */
    public function afterGetAllRates($subject, $result)
    {
        $availableMethods = [
            'tablerate_bestway'
        ];

        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method[] $result */
        /**
         * @var int $key
         * @var \Magento\Quote\Model\Quote\Address\RateResult\Method $rate
         */
        foreach ($result as $key => $rate) {
            $code = $rate->getCarrier() . '_' . $rate->getMethod();
            if (!in_array($code, $availableMethods)) {
                unset($result[$key]);
            }
        }

        return $result;
    }
}

Result:

cart page result

checkout page result

All you need to update is namespaces and the $availableMethods array. Data in the $availableMethods array must be captured from the backend setting of your module (system configuration setting).

Update:

If you need to display an error message for unavailable shipping methods you can use next part of code:

/** @var \Magento\Quote\Model\Quote\Address\RateResult\Error $error */
$error = $this->errorFactory->create();
$error->setCarrier($rate->getCarrier());
$error->setMethod($rate->getMethod());
$error->setCarrierTitle($rate->getCarrierTitle());
$error->setErrorMessage(__('Error message here'));
$result[$key] = $error;
  • is there any way return a result with error message like this prnt.sc/q72ard – divya sekar Dec 6 '19 at 10:59
  • 1
    @divyasekar Yes, I've add desired part of code to my answer. – Siarhey Uchukhlebau Dec 6 '19 at 11:39
  • thank you so much you saved my time – divya sekar Dec 6 '19 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.