1

I'm using Magento 2.1 I'm trying to hide the shipping method based on product attribute. I followed the solution found here Hiding Shipping Method for current Logged in/Guest user if a condition method

But as in the comment provided both the shipping methods are disappearing and "Sorry, no quotes are available for this order at this time" are displaying under shipping Methods.

I found the following error in console

{"messages":{"error":[{"code":500,"message":"Fatal Error: 'Uncaught TypeError: Argument 1 passed to Magento\OfflineShipping\Model\Carrier\Flatrate::collectRates() must be an instance of Magento\Quote\Model\Quote\Address\RateRequest,none given in /home/my_server/public_html/vendor/magento/module-offline-shipping/Model/Carrier/Flatrate.php:64\

My Plugin file Flatrate.php is as follows

<?php

namespace MyVendor\Module\Plugin;
use Magento\Quote\Model\Quote\Address\RateCollectorInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;

class Flatrate
{
    public function aroundCollectRates(
    \Magento\OfflineShipping\Model\Carrier\Flatrate $subject,
    \Closure $proceed,
    \Magento\Quote\Model\Quote\Address\RateRequest $request
    ) {
        $items = $request->getAllItems();
        foreach ($items as $item) {
            if ($item->getProduct()->getAttributeCode()=='ups') {
                 //if your attribute value is true don't show this method
                 return false;
            }
        }
        return $proceed();
    }
}

Please help.

2

When you use plugin with around method and if the function have arguments you need to pass it in $proceed Callback method.

<?php

namespace MyVendor\Module\Plugin;
use Magento\Quote\Model\Quote\Address\RateCollectorInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;

class Flatrate
{
    public function aroundCollectRates(
    \Magento\OfflineShipping\Model\Carrier\Flatrate $subject,
    \Closure $proceed,
    \Magento\Quote\Model\Quote\Address\RateRequest $request
    ) {
        $items = $request->getAllItems();
        foreach ($items as $item) {
            if ($item->getProduct()->getAttributeCode()=='ups') {
                 //if your attribute value is true don't show this method
                 return false;
            }
        }
        return $proceed($request);
    }
}
  • Thanks... The 500 internal server error got resolved. But still, my Flatrate shipping method is not getting hidden. Did you have any idea about that? – DEEPESH KUMAR R Feb 22 '17 at 9:03
  • It is depend on what condition you want to hide. Please explain it so i can reply on it. – Salma Saiyad Feb 22 '17 at 9:49
  • Here I'm trying to check the attribute code of the products in the cart here if ($item->getProduct()->getAttributeCode()=='ups') if any of the products in the cart having "ups" product attribute it should hide the flatrate shipping method. otherwise display flatrate only – DEEPESH KUMAR R Feb 22 '17 at 9:52
  • first of all check $item->getProduct()->getAttributeCode() what value it returns – Salma Saiyad Feb 22 '17 at 10:02
  • Yes.. I checked that. Its returning null.. Sorry I'm new to magento – DEEPESH KUMAR R Feb 22 '17 at 10:07

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.