0

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" />
</type>

And

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

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

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

    foreach ($allItems as $item) {  
       $_product = $this->product->create()->load($item->getProduct()->getId());
       foreach ($ruleCollection as $rule) {
          if($rule->getConditions()->validate($_product)){
              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.

0

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.