1

Hi I have create custom shipping method. I have enabled it from back end. Now I want show this only if product custom attribute match some conditions.

1

2 Answers 2

2

It's your custom shipping method which have collectRates method in somewhere in model folder ex. for Flatrate shipping method collectRates method is located at Magento\OfflineShipping\Model\Carrier\Flatrate

public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }
    if($customeProductConditionFalse)
       {
        return false;
       }
    ........
}

In this method you can check your condition for product custom attribute match or not and

return false;

to disable shipping method

5
  • i can add product attribute condition but what is the code for disable shipping method. Commented Mar 27, 2018 at 12:30
  • you need to send return false to disable it Commented Mar 27, 2018 at 13:30
  • Is this working for you? I do not want to disable shipping method. But I want to hide the shipping method on some product custom option condition. Commented Jul 7, 2018 at 9:17
  • @DhadukMitesh Did you find the solution for this? Commented May 14, 2019 at 11:17
  • @PratikKamani Please check my answer. Commented May 14, 2019 at 12:48
0

Create 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">
    <type name="Magento\Quote\Api\ShipmentEstimationInterface">
        <plugin name="hide_show_shipping_methods" type="Vendor\Module\Plugin\Quote\Api\ShipmentEstimationPlugin"/>
    </type>
</config>

Create Vendor\Module\Plugin\Quote\Api\ShipmentEstimationPlugin.php.

<?php

namespace Vendor\Module\Plugin\Quote\Api;

use Magento\Quote\Api\ShipmentEstimationInterface;

class ShipmentEstimationPlugin
{
    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }

    public function aroundEstimateByExtendedAddress(
        ShipmentEstimationInterface $subject,
        \Closure $proceed,
        $cartId,
        \Magento\Quote\Api\Data\AddressInterface $address
    ) {
        $shippingMethods = $proceed($cartId, $address);
        if ($this->customerSession->isLoggedIn()) {
            foreach ($shippingMethods as $key => $shippingMethod) {
                //Replace 'freeshipping' with your shipping method which you want to hide
                if ($shippingMethod->getMethodCode() == 'freeshipping') {
                    unset($shippingMethods[$key]);
                }
            }

            return $shippingMethods;
        }
    }
}

It will work for you.

2
  • i tried your plugin for all user but its not works Commented Dec 6, 2019 at 9:58
  • What issue you face? Commented Dec 9, 2019 at 11:55

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.