I am using plugin for the same pusrpose.
<type name="MagentoShippingModelShipping">
<plugin name="hide_shipping_method_based_on_product_attribute" type="VendorShippingRestrictionsPluginMagentoShippingModelProductAttributeBasedShipping" disabled="false" />
</type>
And
public function aroundCollectCarrierRates(
MagentoShippingModelShipping $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.