Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ScopedTierPriceManagement.php
Go to the documentation of this file.
1 <?php
8 
12 
14 {
18  private $productRepository;
19 
23  private $storeManager;
24 
28  private $config;
29 
33  private $priceModifier;
34 
38  private $tierPriceManagement;
39 
47  public function __construct(
48  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
49  \Magento\Store\Model\StoreManagerInterface $storeManager,
50  \Magento\Framework\App\Config\ScopeConfigInterface $config,
51  PriceModifier $priceModifier,
52  TierPriceManagement $tierPriceManagement
53  ) {
54  $this->productRepository = $productRepository;
55  $this->storeManager = $storeManager;
56  $this->priceModifier = $priceModifier;
57  $this->config = $config;
58  $this->tierPriceManagement = $tierPriceManagement;
59  }
60 
64  public function add($sku, ProductTierPriceInterface $tierPrice)
65  {
66  $product = $this->productRepository->get($sku, ['edit_mode' => true]);
67  $product->setTierPrices(
68  $this->prepareTierPrices($product->getTierPrices(), $tierPrice)
69  );
70  try {
71  $this->productRepository->save($product);
72  } catch (\Exception $e) {
73  throw new \Magento\Framework\Exception\CouldNotSaveException(__("The group price couldn't be saved."));
74  }
75  return true;
76  }
77 
83  private function prepareTierPrices(array $tierPrices, ProductTierPriceInterface $tierPrice)
84  {
85  $this->validate($tierPrice);
86  $websiteId = $this->getWebsiteId();
87 
88  foreach ($tierPrices as $index => $item) {
89  $tierPriceWebsite = $tierPrice->getExtensionAttributes()
90  ? $tierPrice->getExtensionAttributes()->getWebsiteId()
91  : 0;
92 
93  if ($item->getCustomerGroupId() == $tierPrice->getCustomerGroupId()
94  && $websiteId == $tierPriceWebsite
95  && $item->getQty() == $tierPrice->getQty()
96  ) {
97  unset($tierPrices[$index]);
98  break;
99  }
100  }
101 
102  $tierPrices[] = $tierPrice;
103  return $tierPrices;
104  }
105 
109  private function getWebsiteId()
110  {
111  $websiteIdentifier = 0;
112  $value = $this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
113  if ($value != 0) {
114  $websiteIdentifier = $this->storeManager->getWebsite()->getId();
115  }
116 
117  return $websiteIdentifier;
118  }
119 
125  private function validate(ProductTierPriceInterface $tierPrice)
126  {
127  $data = ['qty' => $tierPrice->getQty(), 'price' => $tierPrice->getValue()];
128  foreach ($data as $value) {
129  if (!is_float($value) || $value <= 0) {
130  throw new \Magento\Framework\Exception\InputException(
131  __('The data was invalid. Verify the data and try again.')
132  );
133  }
134  }
135  }
136 
140  public function remove($sku, ProductTierPriceInterface $tierPrice)
141  {
142  $product = $this->productRepository->get($sku, ['edit_mode' => true]);
143  $this->priceModifier->removeTierPrice(
144  $product,
145  $tierPrice->getCustomerGroupId(),
146  $tierPrice->getQty(),
147  $this->getWebsiteId()
148  );
149  return true;
150  }
151 
155  public function getList($sku, $customerGroupId)
156  {
157  return $this->tierPriceManagement->getList($sku, $customerGroupId);
158  }
159 }
$config
Definition: fraud_order.php:17
$storeManager
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $config, PriceModifier $priceModifier, TierPriceManagement $tierPriceManagement)
$index
Definition: list.phtml:44