Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SpecialPriceStorage.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
19  private $specialPriceResource;
20 
24  private $specialPriceFactory;
25 
29  private $productIdLocator;
30 
34  private $storeRepository;
35 
39  private $validationResult;
40 
44  private $invalidSkuProcessor;
45 
49  private $allowedProductTypes = [];
50 
60  public function __construct(
61  \Magento\Catalog\Api\SpecialPriceInterface $specialPriceResource,
62  \Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory $specialPriceFactory,
63  \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator,
64  \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
65  \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult,
66  \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor,
67  array $allowedProductTypes = []
68  ) {
69  $this->specialPriceResource = $specialPriceResource;
70  $this->specialPriceFactory = $specialPriceFactory;
71  $this->productIdLocator = $productIdLocator;
72  $this->storeRepository = $storeRepository;
73  $this->validationResult = $validationResult;
74  $this->invalidSkuProcessor = $invalidSkuProcessor;
75  $this->allowedProductTypes = $allowedProductTypes;
76  }
77 
81  public function get(array $skus)
82  {
83  $skus = $this->invalidSkuProcessor->filterSkuList($skus, $this->allowedProductTypes);
84  $rawPrices = $this->specialPriceResource->get($skus);
85 
86  $prices = [];
87  foreach ($rawPrices as $rawPrice) {
89  $price = $this->specialPriceFactory->create();
90  $sku = isset($rawPrice['sku'])
91  ? $rawPrice['sku']
92  : $this->retrieveSkuById($rawPrice[$this->specialPriceResource->getEntityLinkField()], $skus);
93  $price->setSku($sku);
94  $price->setPrice($rawPrice['value']);
95  $price->setStoreId($rawPrice['store_id']);
96  $price->setPriceFrom($rawPrice['price_from']);
97  $price->setPriceTo($rawPrice['price_to']);
98  $prices[] = $price;
99  }
100 
101  return $prices;
102  }
103 
107  public function update(array $prices)
108  {
109  $prices = $this->retrieveValidPrices($prices);
110  $this->specialPriceResource->update($prices);
111 
112  return $this->validationResult->getFailedItems();
113  }
114 
118  public function delete(array $prices)
119  {
120  $prices = $this->retrieveValidPrices($prices);
121  $this->specialPriceResource->delete($prices);
122 
123  return $this->validationResult->getFailedItems();
124  }
125 
132  private function retrieveValidPrices(array $prices)
133  {
134  $skus = array_unique(
135  array_map(function ($price) {
136  return $price->getSku();
137  }, $prices)
138  );
139  $failedSkus = $this->invalidSkuProcessor->retrieveInvalidSkuList($skus, $this->allowedProductTypes);
140 
141  foreach ($prices as $key => $price) {
142  if (!$price->getSku() || in_array($price->getSku(), $failedSkus)) {
143  $this->validationResult->addFailedItem(
144  $key,
145  __(
146  'The product that was requested doesn\'t exist. Verify the product and try again. '
147  . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
148  [
149  'SKU' => $price->getSku(),
150  'storeId' => $price->getStoreId(),
151  'priceFrom' => $price->getPriceFrom(),
152  'priceTo' => $price->getPriceTo()
153  ]
154  ),
155  [
156  'SKU' => $price->getSku(),
157  'storeId' => $price->getStoreId(),
158  'priceFrom' => $price->getPriceFrom(),
159  'priceTo' => $price->getPriceTo()
160  ]
161  );
162  }
163  $this->checkPrice($price, $key);
164  $this->checkDate($price, $price->getPriceFrom(), 'Price From', $key);
165  $this->checkDate($price, $price->getPriceTo(), 'Price To', $key);
166  try {
167  $this->storeRepository->getById($price->getStoreId());
168  } catch (NoSuchEntityException $e) {
169  $this->validationResult->addFailedItem(
170  $key,
171  __(
172  'Requested store is not found. '
173  . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
174  [
175  'SKU' => $price->getSku(),
176  'storeId' => $price->getStoreId(),
177  'priceFrom' => $price->getPriceFrom(),
178  'priceTo' => $price->getPriceTo()
179  ]
180  ),
181  [
182  'SKU' => $price->getSku(),
183  'storeId' => $price->getStoreId(),
184  'priceFrom' => $price->getPriceFrom(),
185  'priceTo' => $price->getPriceTo()
186  ]
187  );
188  }
189  }
190 
191  foreach ($this->validationResult->getFailedRowIds() as $id) {
192  unset($prices[$id]);
193  }
194 
195  return $prices;
196  }
197 
207  private function checkDate(\Magento\Catalog\Api\Data\SpecialPriceInterface $price, $value, $label, $key)
208  {
209  if ($value && !$this->isCorrectDateValue($value)) {
210  $this->validationResult->addFailedItem(
211  $key,
212  __(
213  'Invalid attribute %label = %priceTo. '
214  . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
215  [
216  'label' => $label,
217  'SKU' => $price->getSku(),
218  'storeId' => $price->getStoreId(),
219  'priceFrom' => $price->getPriceFrom(),
220  'priceTo' => $price->getPriceTo()
221  ]
222  ),
223  [
224  'label' => $label,
225  'SKU' => $price->getSku(),
226  'storeId' => $price->getStoreId(),
227  'priceFrom' => $price->getPriceFrom(),
228  'priceTo' => $price->getPriceTo()
229  ]
230  );
231  }
232  }
233 
242  private function checkPrice(\Magento\Catalog\Api\Data\SpecialPriceInterface $price, $key)
243  {
244  if (null === $price->getPrice() || $price->getPrice() < 0) {
245  $this->validationResult->addFailedItem(
246  $key,
247  __(
248  'Invalid attribute Price = %price. '
249  . 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
250  [
251  'price' => $price->getPrice(),
252  'SKU' => $price->getSku(),
253  'storeId' => $price->getStoreId(),
254  'priceFrom' => $price->getPriceFrom(),
255  'priceTo' => $price->getPriceTo()
256  ]
257  ),
258  [
259  'price' => $price->getPrice(),
260  'SKU' => $price->getSku(),
261  'storeId' => $price->getStoreId(),
262  'priceFrom' => $price->getPriceFrom(),
263  'priceTo' => $price->getPriceTo()
264  ]
265  );
266  }
267  }
268 
276  private function retrieveSkuById($productId, array $skus)
277  {
278  foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $sku => $ids) {
279  if (isset($ids[$productId])) {
280  return $sku;
281  }
282  }
283 
284  return null;
285  }
286 
293  private function isCorrectDateValue($date)
294  {
295  $actualDate = date('Y-m-d H:i:s', strtotime($date));
296  return $actualDate && $actualDate === $date;
297  }
298 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$price
foreach($websiteCodes as $websiteCode) $skus
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
__construct(\Magento\Catalog\Api\SpecialPriceInterface $specialPriceResource, \Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory $specialPriceFactory, \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator, \Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult, \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor, array $allowedProductTypes=[])