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

4
  • Please make Enable status only for table rate shipping method for your front-end website/store Nov 6, 2019 at 13:23
  • not only needed for "Table rate" shipping method and i needed for dynamically selected shipping method shown in frontend Nov 6, 2019 at 13:29
  • Is that your custom field...? Nov 6, 2019 at 13:31
  • yes specific shipping method is custom field Nov 6, 2019 at 13:50

1 Answer 1

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;
3
  • is there any way return a result with error message like this prnt.sc/q72ard Dec 6, 2019 at 10:59
  • 1
    @divyasekar Yes, I've add desired part of code to my answer. Dec 6, 2019 at 11:39
  • thank you so much you saved my time Dec 6, 2019 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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