Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartTotalRepository.php
Go to the documentation of this file.
1 <?php
7 
16 
22 {
28  private $totalsFactory;
29 
35  private $quoteRepository;
36 
40  private $dataObjectHelper;
41 
45  private $itemConverter;
46 
50  protected $couponService;
51 
55  protected $totalsConverter;
56 
65  public function __construct(
66  Api\Data\TotalsInterfaceFactory $totalsFactory,
67  CartRepositoryInterface $quoteRepository,
68  DataObjectHelper $dataObjectHelper,
71  ItemConverter $converter
72  ) {
73  $this->totalsFactory = $totalsFactory;
74  $this->quoteRepository = $quoteRepository;
75  $this->dataObjectHelper = $dataObjectHelper;
76  $this->couponService = $couponService;
77  $this->totalsConverter = $totalsConverter;
78  $this->itemConverter = $converter;
79  }
80 
87  public function get($cartId)
88  {
90  $quote = $this->quoteRepository->getActive($cartId);
91  if ($quote->isVirtual()) {
92  $addressTotalsData = $quote->getBillingAddress()->getData();
93  $addressTotals = $quote->getBillingAddress()->getTotals();
94  } else {
95  $addressTotalsData = $quote->getShippingAddress()->getData();
96  $addressTotals = $quote->getShippingAddress()->getTotals();
97  }
98  unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
99 
101  $quoteTotals = $this->totalsFactory->create();
102  $this->dataObjectHelper->populateWithArray(
103  $quoteTotals,
104  $addressTotalsData,
105  \Magento\Quote\Api\Data\TotalsInterface::class
106  );
107  $items = [];
108  foreach ($quote->getAllVisibleItems() as $index => $item) {
109  $items[$index] = $this->itemConverter->modelToDataObject($item);
110  }
111  $calculatedTotals = $this->totalsConverter->process($addressTotals);
112  $quoteTotals->setTotalSegments($calculatedTotals);
113 
114  $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
115  $amount = $amount > 0 ? $amount : 0;
116  $quoteTotals->setCouponCode($this->couponService->get($cartId));
117  $quoteTotals->setGrandTotal($amount);
118  $quoteTotals->setItems($items);
119  $quoteTotals->setItemsQty($quote->getItemsQty());
120  $quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
121  $quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
122  return $quoteTotals;
123  }
124 }
$quote
$amount
Definition: order.php:14
$cartId
Definition: quote.php:22
__construct(Api\Data\TotalsInterfaceFactory $totalsFactory, CartRepositoryInterface $quoteRepository, DataObjectHelper $dataObjectHelper, CouponManagementInterface $couponService, TotalsConverter $totalsConverter, ItemConverter $converter)
$quoteRepository
$index
Definition: list.phtml:44
$items