Skip to content

Call to a member function isVirtual() on null magento2

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(
    MagentoShippingModelShipping $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?