/** * Collect address discount amount * * @param Address $address * @return $this */ public function collect(Address $address) { parent::collect($address); $quote = $address->getQuote(); $store = $this->_storeManager->getStore($quote->getStoreId()); $this->_calculator->reset($address); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $eventArgs = array('website_id' => $store->getWebsiteId(), 'customer_group_id' => $quote->getCustomerGroupId(), 'coupon_code' => $quote->getCouponCode()); $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode()); $this->_calculator->initTotals($items, $address); $address->setDiscountDescription(array()); $items = $this->_calculator->sortItemsByPriority($items); foreach ($items as $item) { if ($item->getNoDiscount()) { $item->setDiscountAmount(0); $item->setBaseDiscountAmount(0); continue; } /** * Child item discount we calculate for parent */ if ($item->getParentItemId()) { continue; } $eventArgs['item'] = $item; $this->_eventManager->dispatch('sales_quote_address_discount_item', $eventArgs); if ($item->getHasChildren() && $item->isChildrenCalculated()) { $isMatchedParent = $this->_calculator->canApplyRules($item); $this->_calculator->setSkipActionsValidation($isMatchedParent); foreach ($item->getChildren() as $child) { $this->_calculator->process($child); if ($isMatchedParent) { $this->_recalculateChildDiscount($child); } $eventArgs['item'] = $child; $this->_eventManager->dispatch('sales_quote_address_discount_item', $eventArgs); $this->_aggregateItemDiscount($child); } $this->_calculator->setSkipActionsValidation(false); } else { $this->_calculator->process($item); $this->_aggregateItemDiscount($item); } } /** * Process shipping amount discount */ $address->setShippingDiscountAmount(0); $address->setBaseShippingDiscountAmount(0); if ($address->getShippingAmount()) { $this->_calculator->processShippingAmount($address); $this->_addAmount(-$address->getShippingDiscountAmount()); $this->_addBaseAmount(-$address->getBaseShippingDiscountAmount()); } $this->_calculator->prepareDescription($address); return $this; }
/** * Collect address subtotal * * @param Address $address * @return $this */ public function collect(Address $address) { parent::collect($address); $address->setTotalQty(0); $baseVirtualAmount = $virtualAmount = 0; /** * Process address items */ $items = $this->_getAddressItems($address); foreach ($items as $item) { if ($this->_initItem($address, $item) && $item->getQty() > 0) { /** * Separately calculate subtotal only for virtual products */ if ($item->getProduct()->isVirtual()) { $virtualAmount += $item->getRowTotal(); $baseVirtualAmount += $item->getBaseRowTotal(); } } else { $this->_removeItem($address, $item); } } $address->setBaseVirtualAmount($baseVirtualAmount); $address->setVirtualAmount($virtualAmount); /** * Initialize grand totals */ $this->_salesData->checkQuoteAmount($address->getQuote(), $address->getSubtotal()); $this->_salesData->checkQuoteAmount($address->getQuote(), $address->getBaseSubtotal()); return $this; }
/** * Collect Weee amounts for the quote / order * * @param \Magento\Sales\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Sales\Model\Quote\Address $address) { \Magento\Sales\Model\Quote\Address\Total\AbstractTotal::collect($address); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $address->setAppliedTaxesReset(true); $address->setAppliedTaxes(array()); $this->_store = $address->getQuote()->getStore(); foreach ($items as $item) { if ($item->getParentItemId()) { continue; } $this->_resetItemData($item); if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $this->_resetItemData($child); $this->_process($address, $child); } $this->_recalculateParent($item); } else { $this->_process($address, $item); } } return $this; }
/** * Collect recurring item parameters and copy to the address items * * @param \Magento\Sales\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Sales\Model\Quote\Address $address) { parent::collect($address); $items = $this->_getAddressItems($address); foreach ($items as $item) { if ($item->getProduct()->getIsRecurring()) { $paymentData = $item->getProduct()->getRecurringPayment(); if (!empty($paymentData[$this->_paymentDataKey])) { $item->setData($this->_itemRowTotalKey, $this->priceCurrency->convert($paymentData[$this->_paymentDataKey], $address->getQuote()->getStore())); $this->_afterCollectSuccess($address, $item); } } } return $this; }
/** * Collect information about MSRP price enabled * * @param \Magento\Sales\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Sales\Model\Quote\Address $address) { parent::collect($address); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $canApplyMsrp = false; foreach ($items as $item) { if (!$item->getParentItemId() && $this->_catalogData->canApplyMsrp($item->getProductId(), \Magento\Catalog\Model\Product\Attribute\Source\Msrp\Type::TYPE_BEFORE_ORDER_CONFIRM, true)) { $canApplyMsrp = true; break; } } $address->setCanApplyMsrp($canApplyMsrp); return $this; }
/** * 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; }
/** * Collect Weee taxes amount and prepare items prices for taxation and discount * * @param \Magento\Sales\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Sales\Model\Quote\Address $address) { \Magento\Sales\Model\Quote\Address\Total\AbstractTotal::collect($address); $this->store = $address->getQuote()->getStore(); if (!$this->weeeData->isEnabled($this->_store)) { return $this; } $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } //If Weee is not taxable, then the 'weee' collector has accumulated the non-taxable total values if (!$this->weeeData->isTaxable($this->_store)) { //Because Weee is not taxable: Weee excluding tax == Weee including tax $weeeTotal = $address->getWeeeTotalExclTax(); $weeeBaseTotal = $address->getWeeeBaseTotalExclTax(); //Add to appropriate 'subtotal' or 'weee' accumulators $this->processTotalAmount($address, $weeeTotal, $weeeBaseTotal, $weeeTotal, $weeeBaseTotal); return $this; } $weeeCodeToItemMap = $address->getWeeeCodeToItemMap(); $extraTaxableDetails = $address->getExtraTaxableDetails(); if (isset($extraTaxableDetails[self::ITEM_TYPE])) { foreach ($extraTaxableDetails[self::ITEM_TYPE] as $itemCode => $weeeAttributesTaxDetails) { $weeeCode = $weeeAttributesTaxDetails[0]['code']; $item = $weeeCodeToItemMap[$weeeCode]; $this->weeeData->setApplied($item, []); $productTaxes = []; $totalValueInclTax = 0; $baseTotalValueInclTax = 0; $totalRowValueInclTax = 0; $baseTotalRowValueInclTax = 0; $totalValueExclTax = 0; $baseTotalValueExclTax = 0; $totalRowValueExclTax = 0; $baseTotalRowValueExclTax = 0; //Process each weee attribute of an item foreach ($weeeAttributesTaxDetails as $weeeTaxDetails) { $weeeCode = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_CODE]; $attributeCode = explode('-', $weeeCode)[1]; $valueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_PRICE_EXCL_TAX]; $baseValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_PRICE_EXCL_TAX]; $valueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_PRICE_INCL_TAX]; $baseValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_PRICE_INCL_TAX]; $rowValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_ROW_TOTAL]; $baseRowValueExclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_ROW_TOTAL]; $rowValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_ROW_TOTAL_INCL_TAX]; $baseRowValueInclTax = $weeeTaxDetails[CommonTaxCollector::KEY_TAX_DETAILS_BASE_ROW_TOTAL_INCL_TAX]; $totalValueInclTax += $valueInclTax; $baseTotalValueInclTax += $baseValueInclTax; $totalRowValueInclTax += $rowValueInclTax; $baseTotalRowValueInclTax += $baseRowValueInclTax; $totalValueExclTax += $valueExclTax; $baseTotalValueExclTax += $baseValueExclTax; $totalRowValueExclTax += $rowValueExclTax; $baseTotalRowValueExclTax += $baseRowValueExclTax; $productTaxes[] = array('title' => $attributeCode, 'base_amount' => $baseValueExclTax, 'amount' => $valueExclTax, 'row_amount' => $rowValueExclTax, 'base_row_amount' => $baseRowValueExclTax, 'base_amount_incl_tax' => $baseValueInclTax, 'amount_incl_tax' => $valueInclTax, 'row_amount_incl_tax' => $rowValueInclTax, 'base_row_amount_incl_tax' => $baseRowValueInclTax); } $item->setWeeeTaxAppliedAmount($totalValueExclTax)->setBaseWeeeTaxAppliedAmount($baseTotalValueExclTax)->setWeeeTaxAppliedRowAmount($totalRowValueExclTax)->setBaseWeeeTaxAppliedRowAmnt($baseTotalRowValueExclTax); $item->setWeeeTaxAppliedAmountInclTax($totalValueInclTax)->setBaseWeeeTaxAppliedAmountInclTax($baseTotalValueInclTax)->setWeeeTaxAppliedRowAmountInclTax($totalRowValueInclTax)->setBaseWeeeTaxAppliedRowAmntInclTax($baseTotalRowValueInclTax); $this->processTotalAmount($address, $totalRowValueExclTax, $baseTotalRowValueExclTax, $totalRowValueInclTax, $baseTotalRowValueInclTax); $this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes)); } } return $this; }
/** * Collect totals information about shipping * * @param \Magento\Sales\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Sales\Model\Quote\Address $address) { parent::collect($address); $address->setWeight(0); $address->setFreeMethodWeight(0); $this->_setAmount(0)->_setBaseAmount(0); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $method = $address->getShippingMethod(); $freeAddress = $address->getFreeShipping(); $addressWeight = $address->getWeight(); $freeMethodWeight = $address->getFreeMethodWeight(); $addressQty = 0; foreach ($items as $item) { /** * Skip if this item is virtual */ if ($item->getProduct()->isVirtual()) { continue; } /** * Children weight we calculate for parent */ if ($item->getParentItem()) { continue; } if ($item->getHasChildren() && $item->isShipSeparately()) { foreach ($item->getChildren() as $child) { if ($child->getProduct()->isVirtual()) { continue; } $addressQty += $child->getTotalQty(); if (!$item->getProduct()->getWeightType()) { $itemWeight = $child->getWeight(); $itemQty = $child->getTotalQty(); $rowWeight = $itemWeight * $itemQty; $addressWeight += $rowWeight; if ($freeAddress || $child->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($child->getFreeShipping())) { $freeQty = $child->getFreeShipping(); if ($itemQty > $freeQty) { $rowWeight = $itemWeight * ($itemQty - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $item->setRowWeight($rowWeight); } } if ($item->getProduct()->getWeightType()) { $itemWeight = $item->getWeight(); $rowWeight = $itemWeight * $item->getQty(); $addressWeight += $rowWeight; if ($freeAddress || $item->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($item->getFreeShipping())) { $freeQty = $item->getFreeShipping(); if ($item->getQty() > $freeQty) { $rowWeight = $itemWeight * ($item->getQty() - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $item->setRowWeight($rowWeight); } } else { if (!$item->getProduct()->isVirtual()) { $addressQty += $item->getQty(); } $itemWeight = $item->getWeight(); $rowWeight = $itemWeight * $item->getQty(); $addressWeight += $rowWeight; if ($freeAddress || $item->getFreeShipping() === true) { $rowWeight = 0; } elseif (is_numeric($item->getFreeShipping())) { $freeQty = $item->getFreeShipping(); if ($item->getQty() > $freeQty) { $rowWeight = $itemWeight * ($item->getQty() - $freeQty); } else { $rowWeight = 0; } } $freeMethodWeight += $rowWeight; $item->setRowWeight($rowWeight); } } if (isset($addressQty)) { $address->setItemQty($addressQty); } $address->setWeight($addressWeight); $address->setFreeMethodWeight($freeMethodWeight); $address->collectShippingRates(); $this->_setAmount(0)->_setBaseAmount(0); if ($method) { foreach ($address->getAllShippingRates() as $rate) { if ($rate->getCode() == $method) { $amountPrice = $this->priceCurrency->convert($rate->getPrice(), $address->getQuote()->getStore()); $this->_setAmount($amountPrice); $this->_setBaseAmount($rate->getPrice()); $shippingDescription = $rate->getCarrierTitle() . ' - ' . $rate->getMethodTitle(); $address->setShippingDescription(trim($shippingDescription, ' -')); break; } } } return $this; }
/** * Collect Weee amounts for the quote / order * * @param \Magento\Sales\Model\Quote\Address $address * @return $this */ public function collect(\Magento\Sales\Model\Quote\Address $address) { AbstractTotal::collect($address); $this->_store = $address->getQuote()->getStore(); if (!$this->weeeData->isEnabled($this->_store)) { return $this; } $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $this->weeeTotalExclTax = 0; $this->weeeBaseTotalExclTax = 0; foreach ($items as $item) { if ($item->getParentItemId()) { continue; } $this->_resetItemData($item); if ($item->getHasChildren() && $item->isChildrenCalculated()) { foreach ($item->getChildren() as $child) { $this->_resetItemData($child); $this->_process($address, $child); } $this->_recalculateParent($item); } else { $this->_process($address, $item); } } $address->setWeeeCodeToItemMap($this->weeeCodeToItemMap); $address->setWeeeTotalExclTax($this->weeeTotalExclTax); $address->setWeeeBaseTotalExclTax($this->weeeBaseTotalExclTax); return $this; }
/** * Collect tax totals for quote address * * @param Address $address * @return $this */ public function collect(Address $address) { parent::collect($address); $items = $this->_getAddressItems($address); if (!$items) { return $this; } //Preparation for calling taxCalculationService with base currency $quoteDetails = $this->prepareQuoteDetails($address, true); $baseTaxDetailsBase = $this->taxCalculationService->calculateTax($quoteDetails, $address->getQuote()->getStore()->getStoreId()); //Preparation for calling taxCalculationService with display currency $quoteDetails = $this->prepareQuoteDetails($address, false); $taxDetails = $this->taxCalculationService->calculateTax($quoteDetails, $address->getQuote()->getStore()->getStoreId()); //Populate address and items with tax calculation results $this->updateTaxInfo($address, $taxDetails, $baseTaxDetailsBase); if ($this->processExtraSubtotalAmount()) { $address->addTotalAmount('tax', $address->getExtraTaxAmount()); $address->addBaseTotalAmount('tax', $address->getBaseExtraTaxAmount()); $address->addTotalAmount('subtotal', $address->getExtraSubtotalAmount()); $address->addBaseTotalAmount('subtotal', $address->getBaseExtraSubtotalAmount()); $address->setSubtotalInclTax($address->getSubtotalInclTax() + $address->getExtraSubtotalAmount() + $address->getExtraTaxAmount()); $address->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax() + $address->getBaseExtraSubtotalAmount() + $address->getBaseExtraTaxAmount()); } return $this; }