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 
9 
15 {
19  private $extensionFactory;
20 
24  private $ruleRepository;
25 
29  private $coupon;
30 
34  private $storeManager;
35 
43  public function __construct(
44  \Magento\Quote\Api\Data\TotalsExtensionFactory $extensionFactory,
45  \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository,
46  \Magento\SalesRule\Model\Coupon $coupon,
47  StoreManagerInterface $storeManager
48  ) {
49  $this->extensionFactory = $extensionFactory;
50  $this->ruleRepository = $ruleRepository;
51  $this->coupon = $coupon;
52  $this->storeManager = $storeManager;
53  }
54 
62  public function afterGet(
63  \Magento\Quote\Model\Cart\CartTotalRepository $subject,
64  \Magento\Quote\Api\Data\TotalsInterface $result
65  ) {
66  if ($result->getExtensionAttributes() === null) {
67  $extensionAttributes = $this->extensionFactory->create();
68  $result->setExtensionAttributes($extensionAttributes);
69  }
70 
71  $extensionAttributes = $result->getExtensionAttributes();
72  $couponCode = $result->getCouponCode();
73 
74  if (empty($couponCode)) {
75  return $result;
76  }
77 
78  $this->coupon->loadByCode($couponCode);
79  $ruleId = $this->coupon->getRuleId();
80 
81  if (empty($ruleId)) {
82  return $result;
83  }
84 
85  $storeId = $this->storeManager->getStore()->getId();
86  $rule = $this->ruleRepository->getById($ruleId);
87 
88  $storeLabel = $storeLabelFallback = null;
89 
90  /* @var $label \Magento\SalesRule\Model\Data\RuleLabel */
91  foreach ($rule->getStoreLabels() as $label) {
92  if ($label->getStoreId() === 0) {
93  $storeLabelFallback = $label->getStoreLabel();
94  }
95 
96  if ($label->getStoreId() == $storeId) {
97  $storeLabel = $label->getStoreLabel();
98  break;
99  }
100  }
101 
102  $extensionAttributes->setCouponLabel(($storeLabel) ? $storeLabel : $storeLabelFallback);
103  $result->setExtensionAttributes($extensionAttributes);
104  return $result;
105  }
106 }
afterGet(\Magento\Quote\Model\Cart\CartTotalRepository $subject, \Magento\Quote\Api\Data\TotalsInterface $result)
$coupon
$storeManager
__construct(\Magento\Quote\Api\Data\TotalsExtensionFactory $extensionFactory, \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository, \Magento\SalesRule\Model\Coupon $coupon, StoreManagerInterface $storeManager)
$label
Definition: details.phtml:21
$extensionAttributes
Definition: payment.php:22
$ruleRepository