0

I have some product condition that need to be validate by core validate() function, I am using plugin here to restrict free shipping method if certain conditions are there in the cart.

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

    $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 == 'freeshipping') {
                 return false;
              }
           }
        }
    }           
    $result = $proceed($carrierCode, $request);
    return $result;
}

I need to validate custom rule but I am getting Error: Call to a member function isVirtual() on null issue. Plz let me know how to fix this issue?

2 Answers 2

0

To solve this issue you need to check if $item is not null before accessing its methods.

Check this updated code,

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

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

    foreach ($allItems as $item) {    
         if ($item === null) {
                  continue; // skip to next iteration if item is null
              }
    
        $_product = $this->product->create()->load($item->getProduct()->getId());
        foreach ($ruleCollection as $rule) {
           if($rule->getConditions()->validate($_product)){
              if $carrierCode == 'freeshipping') {
                 return false;
              }
           }
        }
    }           
    $result = $proceed($carrierCode, $request);
    return $result;
}
6
  • thanks for your answer but it is not working, I have already tried this solution. I am getting issue in this way. Error: Call to a member function isVirtual() on null in vendor\magento\module-sales-rule\Model\Rule\Condition\Address.php:177 Mar 29 at 12:34
  • @AbhayAgrawal same issue here. Did you find a solution please?
    – G. G.
    May 26 at 0:42
  • @G.G. Yes I have solved the issue May 29 at 12:45
  • @G.G. I am not getting any specific product information, I am validating some shipping rule, it is working as per the above comment May 29 at 12:51
  • @AbhayAgrawal not working on my end. Can you help me with my code please?
    – G. G.
    Jun 5 at 16:58
0
foreach ($allItems as $item) {  
foreach ($ruleCollection as $rule) {
    if($rule->getConditions()->validate($item)) {
        if (in_array($carrierCode, $disableShippingMethod)) {
            return false;
        }
    }
}

}

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.