cartRepository = $cartRepository; $this->cartTotalRepository = $cartTotalRepository; } /** * {@inheritDoc} */ public function calculate( $cartId, \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation ) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->cartRepository->get($cartId); $this->validateQuote($quote); if ($quote->getIsVirtual()) { $quote->setBillingAddress($addressInformation->getAddress()); } else { $quote->setShippingAddress($addressInformation->getAddress()); $quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod( $addressInformation->getShippingCarrierCode() . '_' . $addressInformation->getShippingMethodCode() ); } $quote->collectTotals(); return $this->cartTotalRepository->get($cartId); } /** * @param \Magento\Quote\Model\Quote $quote * @throws \Magento\Framework\Exception\LocalizedException * @return void */ protected function validateQuote(\Magento\Quote\Model\Quote $quote) { if ($quote->getItemsCount() === 0) { throw new \Magento\Framework\Exception\LocalizedException( __('Totals calculation is not applicable to empty cart') ); } } }