/** * {@inheritDoc} */ public function isFreeShipping(\Magento\Quote\Model\Quote $quote, $items) { /** @var \Magento\Quote\Api\Data\CartItemInterface[] $items */ if (!count($items)) { return false; } $addressFreeShipping = true; $store = $this->storeManager->getStore($quote->getStoreId()); $this->calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode()); /** @var \Magento\Quote\Api\Data\CartItemInterface $item */ foreach ($items as $item) { if ($item->getNoDiscount()) { $addressFreeShipping = false; $item->setFreeShipping(false); continue; } /** Child item discount we calculate for parent */ if ($item->getParentItemId()) { continue; } $this->calculator->processFreeShipping($item); $itemFreeShipping = (bool) $item->getFreeShipping(); $addressFreeShipping = $addressFreeShipping && $itemFreeShipping; /** Parent free shipping we apply to all children*/ $this->applyToChildren($item, $itemFreeShipping); } return $addressFreeShipping; }
/** * Collect information about free shipping for all address items * * @param \Magento\Sales\Model\Quote\Address $address * @return \Magento\OfflineShipping\Model\Quote\Freeshipping */ public function collect(Address $address) { parent::collect($address); $quote = $address->getQuote(); $store = $this->_storeManager->getStore($quote->getStoreId()); $address->setFreeShipping(0); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode()); $isAllFree = true; foreach ($items as $item) { if ($item->getNoDiscount()) { $isAllFree = false; $item->setFreeShipping(false); } else { /** * Child item discount we calculate for parent */ if ($item->getParentItemId()) { continue; } $this->_calculator->processFreeShipping($item); $isItemFree = (bool) $item->getFreeShipping(); $isAllFree = $isAllFree && $isItemFree; if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $this->_calculator->processFreeShipping($child); /** * Parent free shipping we apply to all children */ if ($isItemFree) { $child->setFreeShipping($isItemFree); } } } } } if ($isAllFree && !$address->getFreeShipping()) { $address->setFreeShipping(true); } return $this; }