0
  • I want to remove Some of shipping method on both estimate and checkout page.
  • If parameter in quote remove_all to 1 then remove all method which are available and set only one which i have created.
  • if remove_all is 0 then remove just my custom shipping method keep which all are available

I think there should be an observer event when all shipping methods are render and i can remove from there.

4
  • Yes, we know your requirements :) but what you tried to achieve this? or you are looking for a direct solution from someone?
    – Keyur Shah
    Apr 29, 2019 at 12:51
  • i'm trying after method aftercollectRates but nothing help Apr 29, 2019 at 12:59
  • So please mentioned what you tried far so someone can understand :)
    – Keyur Shah
    Apr 29, 2019 at 13:00
  • but i dont think it helps may be i dont know any observer should be there Apr 29, 2019 at 13:01

1 Answer 1

2

You can use next plugin for your purpose:

<type name="Magento\Shipping\Model\Rate\Result">
    <plugin name="your_plugin_name_update_rate_result"
            type="Vendor\Module\Plugin\ShippingRateResultAppend"
            sortOrder="10"
            disabled="false" />
</type>

code will be like this one, but with own validation:

/**
 * Disable the marked shipping rates. 
 * 
 * @param \Magento\Shipping\Model\Rate\Result $subject
 * @param array $result
 * @return array
 */
public function afterGetAllRates($subject, $result)
{
    /** @var \Magento\Quote\Model\Quote $quote */
    $quote = $this->getQuote(); // method must be defined in your class; you can get an actual quote from a session;
    if (!$quote) {
        // exception?
    }
    $removeAll                = (int)$quote->getRemoveAll();
    $customShippingMethodCode = 'custom_methodcode'; // static

    /** @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) {
        $methodCode = $rate->getCarrier() . '_' . $rate->getMethod();
        if ($removeAll && $methodCode !== $customShippingMethodCode) {
            unset($result[$key]);
        } elseif (!$removeAll && $methodCode === $customShippingMethodCode) {
            unset($result[$key]);
        }
    }

    return $result;
}

Your Answer

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

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