Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TotalsInformationManagement.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Checkout\Model;
7 
12 {
19 
25  protected $cartRepository;
26 
32  public function __construct(
35  ) {
36  $this->cartRepository = $cartRepository;
37  $this->cartTotalRepository = $cartTotalRepository;
38  }
39 
43  public function calculate(
44  $cartId,
45  \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation
46  ) {
48  $quote = $this->cartRepository->get($cartId);
49  $this->validateQuote($quote);
50 
51  if ($quote->getIsVirtual()) {
52  $quote->setBillingAddress($addressInformation->getAddress());
53  } else {
54  $quote->setShippingAddress($addressInformation->getAddress());
55  $quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod(
56  $addressInformation->getShippingCarrierCode() . '_' . $addressInformation->getShippingMethodCode()
57  );
58  }
59  $quote->collectTotals();
60 
61  return $this->cartTotalRepository->get($cartId);
62  }
63 
69  protected function validateQuote(\Magento\Quote\Model\Quote $quote)
70  {
71  if ($quote->getItemsCount() === 0) {
72  throw new \Magento\Framework\Exception\LocalizedException(
73  __('Totals calculation is not applicable to empty cart')
74  );
75  }
76  }
77 }
__construct(\Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalRepository)
$quote
__()
Definition: __.php:13
$cartId
Definition: quote.php:22
calculate( $cartId, \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation)