I am using plugin for the same pusrpose.

<type name="Magento\Shipping\Model\Shipping">
    <plugin name="hide_shipping_method_based_on_product_attribute" type="Vendor\ShippingRestrictions\Plugin\Magento\Shipping\Model\ProductAttributeBasedShipping" disabled="false" />


public function aroundCollectCarrierRates(
    \Magento\Shipping\Model\Shipping $subject,
    \Closure $proceed,
) {
    $allItems = $request->getAllItems();

    $storeId = $this->storeManager->getStore()->getId();

    $ruleCollection = $this->collectionFactory->create()
    ->addFieldToFilter('status', ['eq' => 1]);

    foreach ($allItems as $item) {  
       $_product = $this->product->create()->load($item->getProduct()->getId());
       foreach ($ruleCollection as $rule) {
              if ($carrierCode == 'free_shipping')) {
                     return false;
    $result = $proceed($carrierCode, $request);
    return $result;

After that I am getting virtual function null error. I need to restrict free shipping method if certain product attribute condition are find in the cart.


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.

Browse other questions tagged or ask your own question.