totalsFactory = $totalsFactory; $this->quoteRepository = $quoteRepository; $this->dataObjectHelper = $dataObjectHelper; $this->couponService = $couponService; $this->totalsConverter = $totalsConverter; $this->itemConverter = $converter; } /** * @inheritdoc * * @param int $cartId The cart ID. * @return Totals Quote totals data. */ public function get($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual()) { $addressTotalsData = $quote->getBillingAddress()->getData(); $addressTotals = $quote->getBillingAddress()->getTotals(); } else { $addressTotalsData = $quote->getShippingAddress()->getData(); $addressTotals = $quote->getShippingAddress()->getTotals(); } unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]); /** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */ $quoteTotals = $this->totalsFactory->create(); $this->dataObjectHelper->populateWithArray( $quoteTotals, $addressTotalsData, \Magento\Quote\Api\Data\TotalsInterface::class ); $items = []; foreach ($quote->getAllVisibleItems() as $index => $item) { $items[$index] = $this->itemConverter->modelToDataObject($item); } $calculatedTotals = $this->totalsConverter->process($addressTotals); $quoteTotals->setTotalSegments($calculatedTotals); $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount(); $amount = $amount > 0 ? $amount : 0; $quoteTotals->setCouponCode($this->couponService->get($cartId)); $quoteTotals->setGrandTotal($amount); $quoteTotals->setItems($items); $quoteTotals->setItemsQty($quote->getItemsQty()); $quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode()); $quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode()); return $quoteTotals; } }