CartTotalRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Cart;
  7. use Magento\Quote\Api;
  8. use Magento\Quote\Api\CartRepositoryInterface;
  9. use Magento\Quote\Api\CartTotalRepositoryInterface;
  10. use Magento\Catalog\Helper\Product\ConfigurationPool;
  11. use Magento\Framework\Api\DataObjectHelper;
  12. use Magento\Framework\Api\ExtensibleDataInterface;
  13. use Magento\Quote\Model\Cart\Totals\ItemConverter;
  14. use Magento\Quote\Api\CouponManagementInterface;
  15. /**
  16. * Cart totals data object.
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class CartTotalRepository implements CartTotalRepositoryInterface
  20. {
  21. /**
  22. * Cart totals factory.
  23. *
  24. * @var Api\Data\TotalsInterfaceFactory
  25. */
  26. private $totalsFactory;
  27. /**
  28. * Quote repository.
  29. *
  30. * @var \Magento\Quote\Api\CartRepositoryInterface
  31. */
  32. private $quoteRepository;
  33. /**
  34. * @var \Magento\Framework\Api\DataObjectHelper
  35. */
  36. private $dataObjectHelper;
  37. /**
  38. * @var ConfigurationPool
  39. */
  40. private $itemConverter;
  41. /**
  42. * @var CouponManagementInterface
  43. */
  44. protected $couponService;
  45. /**
  46. * @var TotalsConverter
  47. */
  48. protected $totalsConverter;
  49. /**
  50. * @param Api\Data\TotalsInterfaceFactory $totalsFactory
  51. * @param CartRepositoryInterface $quoteRepository
  52. * @param DataObjectHelper $dataObjectHelper
  53. * @param CouponManagementInterface $couponService
  54. * @param TotalsConverter $totalsConverter
  55. * @param ItemConverter $converter
  56. */
  57. public function __construct(
  58. Api\Data\TotalsInterfaceFactory $totalsFactory,
  59. CartRepositoryInterface $quoteRepository,
  60. DataObjectHelper $dataObjectHelper,
  61. CouponManagementInterface $couponService,
  62. TotalsConverter $totalsConverter,
  63. ItemConverter $converter
  64. ) {
  65. $this->totalsFactory = $totalsFactory;
  66. $this->quoteRepository = $quoteRepository;
  67. $this->dataObjectHelper = $dataObjectHelper;
  68. $this->couponService = $couponService;
  69. $this->totalsConverter = $totalsConverter;
  70. $this->itemConverter = $converter;
  71. }
  72. /**
  73. * @inheritdoc
  74. *
  75. * @param int $cartId The cart ID.
  76. * @return Totals Quote totals data.
  77. */
  78. public function get($cartId)
  79. {
  80. /** @var \Magento\Quote\Model\Quote $quote */
  81. $quote = $this->quoteRepository->getActive($cartId);
  82. if ($quote->isVirtual()) {
  83. $addressTotalsData = $quote->getBillingAddress()->getData();
  84. $addressTotals = $quote->getBillingAddress()->getTotals();
  85. } else {
  86. $addressTotalsData = $quote->getShippingAddress()->getData();
  87. $addressTotals = $quote->getShippingAddress()->getTotals();
  88. }
  89. unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
  90. /** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */
  91. $quoteTotals = $this->totalsFactory->create();
  92. $this->dataObjectHelper->populateWithArray(
  93. $quoteTotals,
  94. $addressTotalsData,
  95. \Magento\Quote\Api\Data\TotalsInterface::class
  96. );
  97. $items = [];
  98. foreach ($quote->getAllVisibleItems() as $index => $item) {
  99. $items[$index] = $this->itemConverter->modelToDataObject($item);
  100. }
  101. $calculatedTotals = $this->totalsConverter->process($addressTotals);
  102. $quoteTotals->setTotalSegments($calculatedTotals);
  103. $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
  104. $amount = $amount > 0 ? $amount : 0;
  105. $quoteTotals->setCouponCode($this->couponService->get($cartId));
  106. $quoteTotals->setGrandTotal($amount);
  107. $quoteTotals->setItems($items);
  108. $quoteTotals->setItemsQty($quote->getItemsQty());
  109. $quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
  110. $quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
  111. return $quoteTotals;
  112. }
  113. }