/** * @param \Magento\Framework\Object $request * @return Result|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function collectRates(\Magento\Framework\Object $request) { if (!$this->getConfigFlag('active')) { return false; } $freeBoxes = 0; if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getProduct()->isVirtual() || $item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) { $freeBoxes += $item->getQty() * $child->getQty(); } } } elseif ($item->getFreeShipping()) { $freeBoxes += $item->getQty(); } } } $this->setFreeBoxes($freeBoxes); /** @var Result $result */ $result = $this->_rateResultFactory->create(); if ($this->getConfigData('type') == 'O') { // per order $shippingPrice = $this->getConfigData('price'); } elseif ($this->getConfigData('type') == 'I') { // per item $shippingPrice = $request->getPackageQty() * $this->getConfigData('price') - $this->getFreeBoxes() * $this->getConfigData('price'); } else { $shippingPrice = false; } $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice); if ($shippingPrice !== false) { /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */ $method = $this->_rateMethodFactory->create(); $method->setCarrier('flatrate'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('flatrate'); $method->setMethodTitle($this->getConfigData('name')); if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) { $shippingPrice = '0.00'; } $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); } return $result; }
/** * Validate * * @param \Magento\Framework\Object $object Quote * @return bool */ public function validate(\Magento\Framework\Object $object) { $all = $this->getAggregator() === 'all'; $true = (bool) $this->getValue(); $found = false; foreach ($object->getAllItems() as $item) { $found = $all; foreach ($this->getConditions() as $cond) { $validated = $cond->validate($item); if ($all && !$validated || !$all && $validated) { $found = $validated; break; } } if ($found && $true || !$true && $found) { break; } } // found an item and we're looking for existing one if ($found && $true) { return true; } elseif (!$found && !$true) { // not found and we're making sure it doesn't exist return true; } return false; }
/** * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _initSubtotal() { $store = $this->getStore(); $parent = $this->getParentBlock(); $subtotal = $parent->getTotal('subtotal'); if (!$subtotal) { return $this; } if ($this->_config->displaySalesSubtotalBoth($store)) { $subtotal = (double) $this->_source->getSubtotal(); $baseSubtotal = (double) $this->_source->getBaseSubtotal(); $subtotalIncl = (double) $this->_source->getSubtotalInclTax(); $baseSubtotalIncl = (double) $this->_source->getBaseSubtotalInclTax(); if (!$subtotalIncl || !$baseSubtotalIncl) { // Calculate the subtotal if it is not set $subtotalIncl = $subtotal + $this->_source->getTaxAmount() - $this->_source->getShippingTaxAmount(); $baseSubtotalIncl = $baseSubtotal + $this->_source->getBaseTaxAmount() - $this->_source->getBaseShippingTaxAmount(); if ($this->_source instanceof Order) { // Adjust for the discount tax compensation foreach ($this->_source->getAllItems() as $item) { $subtotalIncl += $item->getHiddenTaxAmount(); $baseSubtotalIncl += $item->getBaseHiddenTaxAmount(); } } } $subtotalIncl = max(0, $subtotalIncl); $baseSubtotalIncl = max(0, $baseSubtotalIncl); $totalExcl = new \Magento\Framework\Object(['code' => 'subtotal_excl', 'value' => $subtotal, 'base_value' => $baseSubtotal, 'label' => __('Subtotal (Excl.Tax)')]); $totalIncl = new \Magento\Framework\Object(['code' => 'subtotal_incl', 'value' => $subtotalIncl, 'base_value' => $baseSubtotalIncl, 'label' => __('Subtotal (Incl.Tax)')]); $parent->addTotal($totalExcl, 'subtotal'); $parent->addTotal($totalIncl, 'subtotal_excl'); $parent->removeTotal('subtotal'); } elseif ($this->_config->displaySalesSubtotalInclTax($store)) { $subtotalIncl = (double) $this->_source->getSubtotalInclTax(); $baseSubtotalIncl = (double) $this->_source->getBaseSubtotalInclTax(); if (!$subtotalIncl) { $subtotalIncl = $this->_source->getSubtotal() + $this->_source->getTaxAmount() - $this->_source->getShippingTaxAmount(); } if (!$baseSubtotalIncl) { $baseSubtotalIncl = $this->_source->getBaseSubtotal() + $this->_source->getBaseTaxAmount() - $this->_source->getBaseShippingTaxAmount(); } $total = $parent->getTotal('subtotal'); if ($total) { $total->setValue(max(0, $subtotalIncl)); $total->setBaseValue(max(0, $baseSubtotalIncl)); } } return $this; }
/** * Collect rates by address * * @param \Magento\Framework\Object $address * @param null|bool|array $limitCarrier * @return $this */ public function collectRatesByAddress(\Magento\Framework\Object $address, $limitCarrier = null) { /** @var $request \Magento\Sales\Model\Quote\Address\RateRequest */ $request = $this->_shipmentRequestFactory->create(); $request->setAllItems($address->getAllItems()); $request->setDestCountryId($address->getCountryId()); $request->setDestRegionId($address->getRegionId()); $request->setDestPostcode($address->getPostcode()); $request->setPackageValue($address->getBaseSubtotal()); $request->setPackageValueWithDiscount($address->getBaseSubtotalWithDiscount()); $request->setPackageWeight($address->getWeight()); $request->setFreeMethodWeight($address->getFreeMethodWeight()); $request->setPackageQty($address->getItemQty()); $request->setStoreId($this->_storeManager->getStore()->getId()); $request->setWebsiteId($this->_storeManager->getStore()->getWebsiteId()); $request->setBaseCurrency($this->_storeManager->getStore()->getBaseCurrency()); $request->setPackageCurrency($this->_storeManager->getStore()->getCurrentCurrency()); $request->setLimitCarrier($limitCarrier); $request->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax()); return $this->collectRates($request); }
/** * @param \Magento\Framework\Object $request * @return \Magento\Shipping\Model\Rate\Result * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function collectRates(\Magento\Framework\Object $request) { if (!$this->getConfigFlag('active')) { return false; } // exclude Virtual products price from Package value if pre-configured if (!$this->getConfigFlag('include_virtual_price') && $request->getAllItems()) { foreach ($request->getAllItems() as $item) { if ($item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getProduct()->isVirtual()) { $request->setPackageValue($request->getPackageValue() - $child->getBaseRowTotal()); } } } elseif ($item->getProduct()->isVirtual()) { $request->setPackageValue($request->getPackageValue() - $item->getBaseRowTotal()); } } } // Free shipping by qty $freeQty = 0; if ($request->getAllItems()) { $freePackageValue = 0; foreach ($request->getAllItems() as $item) { if ($item->getProduct()->isVirtual() || $item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) { $freeShipping = is_numeric($child->getFreeShipping()) ? $child->getFreeShipping() : 0; $freeQty += $item->getQty() * ($child->getQty() - $freeShipping); } } } elseif ($item->getFreeShipping()) { $freeShipping = is_numeric($item->getFreeShipping()) ? $item->getFreeShipping() : 0; $freeQty += $item->getQty() - $freeShipping; $freePackageValue += $item->getBaseRowTotal(); } } $oldValue = $request->getPackageValue(); $request->setPackageValue($oldValue - $freePackageValue); } if (!$request->getConditionName()) { $conditionName = $this->getConfigData('condition_name'); $request->setConditionName($conditionName ? $conditionName : $this->_defaultConditionName); } // Package weight and qty free shipping $oldWeight = $request->getPackageWeight(); $oldQty = $request->getPackageQty(); $request->setPackageWeight($request->getFreeMethodWeight()); $request->setPackageQty($oldQty - $freeQty); /** @var \Magento\Shipping\Model\Rate\Result $result */ $result = $this->_rateResultFactory->create(); $rate = $this->getRate($request); $request->setPackageWeight($oldWeight); $request->setPackageQty($oldQty); if (!empty($rate) && $rate['price'] >= 0) { /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */ $method = $this->_resultMethodFactory->create(); $method->setCarrier('tablerate'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod('bestway'); $method->setMethodTitle($this->getConfigData('name')); if ($request->getFreeShipping() === true || $request->getPackageQty() == $freeQty) { $shippingPrice = 0; } else { $shippingPrice = $this->getFinalPriceWithHandlingFee($rate['price']); } $method->setPrice($shippingPrice); $method->setCost($rate['cost']); $result->append($method); } else { /** @var \Magento\Quote\Model\Quote\Address\RateResult\Error $error */ $error = $this->_rateErrorFactory->create(['data' => ['carrier' => $this->_code, 'carrier_title' => $this->getConfigData('title'), 'error_message' => $this->getConfigData('specificerrmsg')]]); $result->append($error); } return $result; }
/** * Check availability of giftmessages for specified entity. * * @param string $type * @param \Magento\Framework\Object $entity * @param \Magento\Store\Model\Store|int|null $store * @return bool|string|null */ public function isMessagesAvailable($type, \Magento\Framework\Object $entity, $store = null) { if ($type == 'items') { $items = $entity->getAllItems(); if (!is_array($items) || empty($items)) { return $this->_scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); } if ($entity instanceof \Magento\Sales\Model\Quote) { $_type = $entity->getIsMultiShipping() ? 'address_item' : 'item'; } else { $_type = 'order_item'; } foreach ($items as $item) { if ($item->getParentItem()) { continue; } if ($this->isMessagesAvailable($_type, $item, $store)) { return true; } } } elseif ($type == 'item') { return $this->_getDependenceFromStoreConfig($entity->getProduct()->getGiftMessageAvailable(), $store); } elseif ($type == 'order_item') { return $this->_getDependenceFromStoreConfig($entity->getGiftMessageAvailable(), $store); } elseif ($type == 'address_item') { $storeId = is_numeric($store) ? $store : $this->_storeManager->getStore($store)->getId(); if (!$this->isCached('address_item_' . $entity->getProductId())) { $this->setCached('address_item_' . $entity->getProductId(), $this->_productFactory->create()->setStoreId($storeId)->load($entity->getProductId())->getGiftMessageAvailable()); } return $this->_getDependenceFromStoreConfig($this->getCached('address_item_' . $entity->getProductId()), $store); } else { return $this->_scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); } return false; }