Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TierPriceValidator.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  private $productIdLocator;
18 
22  private $searchCriteriaBuilder;
23 
27  private $filterBuilder;
28 
32  private $customerGroupRepository;
33 
37  private $websiteRepository;
38 
42  private $validationResult;
43 
47  private $tierPricePersistence;
48 
54  private $customerGroupsByCode = [];
55 
59  private $invalidSkuProcessor;
60 
66  private $allGroupsValue = 'all groups';
67 
73  private $allWebsitesValue = "0";
74 
80  private $allowedProductTypes = [];
81 
95  public function __construct(
96  \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator,
97  \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
98  \Magento\Framework\Api\FilterBuilder $filterBuilder,
99  \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository,
100  \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository,
101  \Magento\Catalog\Model\Product\Price\TierPricePersistence $tierPricePersistence,
102  \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult,
103  \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor,
104  array $allowedProductTypes = []
105  ) {
106  $this->productIdLocator = $productIdLocator;
107  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
108  $this->filterBuilder = $filterBuilder;
109  $this->customerGroupRepository = $customerGroupRepository;
110  $this->websiteRepository = $websiteRepository;
111  $this->tierPricePersistence = $tierPricePersistence;
112  $this->validationResult = $validationResult;
113  $this->invalidSkuProcessor = $invalidSkuProcessor;
114  $this->allowedProductTypes = $allowedProductTypes;
115  }
116 
123  public function validateSkus(array $skus)
124  {
125  return $this->invalidSkuProcessor->filterSkuList($skus, $this->allowedProductTypes);
126  }
127 
135  public function retrieveValidationResult(array $prices, array $existingPrices = [])
136  {
137  $validationResult = clone $this->validationResult;
138  $skus = array_unique(
139  array_map(function ($price) {
140  return $price->getSku();
141  }, $prices)
142  );
143  $skuDiff = $this->invalidSkuProcessor->retrieveInvalidSkuList($skus, $this->allowedProductTypes);
144  $idsBySku = $this->productIdLocator->retrieveProductIdsBySkus($skus);
145 
146  $pricesBySku = [];
147 
148  foreach ($prices as $price) {
149  $pricesBySku[$price->getSku()][] = $price;
150  }
151 
152  foreach ($prices as $key => $price) {
153  $this->checkSku($price, $key, $skuDiff, $validationResult);
154  $this->checkPrice($price, $key, $validationResult);
155  $ids = isset($idsBySku[$price->getSku()]) ? $idsBySku[$price->getSku()] : [];
156  $this->checkPriceType($price, $ids, $key, $validationResult);
157  $this->checkQuantity($price, $key, $validationResult);
158  $this->checkWebsite($price, $key, $validationResult);
159  if (isset($pricesBySku[$price->getSku()])) {
160  $this->checkUnique($price, $pricesBySku, $key, $validationResult);
161  }
162  $this->checkUnique($price, $existingPrices, $key, $validationResult);
163  $this->checkGroup($price, $key, $validationResult);
164  }
165 
166  return $validationResult;
167  }
168 
178  private function checkSku(
179  \Magento\Catalog\Api\Data\TierPriceInterface $price,
180  $key,
181  array $invalidSkus,
182  Result $validationResult
183  ) {
184  if (!$price->getSku() || in_array($price->getSku(), $invalidSkus)) {
185  $validationResult->addFailedItem(
186  $key,
187  __(
188  'Invalid attribute SKU = %SKU. '
189  . 'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
190  [
191  'SKU' => '%SKU',
192  'websiteId' => '%websiteId',
193  'customerGroup' => '%customerGroup',
194  'qty' => '%qty'
195  ]
196  ),
197  [
198  'SKU' => $price->getSku(),
199  'websiteId' => $price->getWebsiteId(),
200  'customerGroup' => $price->getCustomerGroup(),
201  'qty' => $price->getQuantity()
202  ]
203  );
204  }
205  }
206 
215  private function checkPrice(\Magento\Catalog\Api\Data\TierPriceInterface $price, $key, Result $validationResult)
216  {
217  if (null === $price->getPrice()
218  || $price->getPrice() < 0
220  && $price->getPrice() > 100
221  )
222  ) {
223  $validationResult->addFailedItem(
224  $key,
225  __(
226  'Invalid attribute Price = %price. '
227  . 'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
228  [
229  'price' => '%price',
230  'SKU' => '%SKU',
231  'websiteId' => '%websiteId',
232  'customerGroup' => '%customerGroup',
233  'qty' => '%qty'
234  ]
235  ),
236  [
237  'price' => $price->getPrice(),
238  'SKU' => $price->getSku(),
239  'websiteId' => $price->getWebsiteId(),
240  'customerGroup' => $price->getCustomerGroup(),
241  'qty' => $price->getQuantity()
242  ]
243  );
244  }
245  }
246 
256  private function checkPriceType(
257  \Magento\Catalog\Api\Data\TierPriceInterface $price,
258  array $ids,
259  $key,
260  Result $validationResult
261  ) {
262  if (!in_array(
263  $price->getPriceType(),
264  [
267  ]
268  )
269  || (array_search(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE, $ids)
271  ) {
272  $validationResult->addFailedItem(
273  $key,
274  __(
275  'Invalid attribute Price Type = %priceType. '
276  . 'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
277  [
278  'price' => '%price',
279  'SKU' => '%SKU',
280  'websiteId' => '%websiteId',
281  'customerGroup' => '%customerGroup',
282  'qty' => '%qty'
283  ]
284  ),
285  [
286  'priceType' => $price->getPriceType(),
287  'SKU' => $price->getSku(),
288  'websiteId' => $price->getWebsiteId(),
289  'customerGroup' => $price->getCustomerGroup(),
290  'qty' => $price->getQuantity()
291  ]
292  );
293  }
294  }
295 
304  private function checkQuantity(\Magento\Catalog\Api\Data\TierPriceInterface $price, $key, Result $validationResult)
305  {
306  if ($price->getQuantity() < 1) {
307  $validationResult->addFailedItem(
308  $key,
309  __(
310  'Invalid attribute Quantity = %qty. '
311  . 'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
312  [
313  'SKU' => '%SKU',
314  'websiteId' => '%websiteId',
315  'customerGroup' => '%customerGroup',
316  'qty' => '%qty'
317  ]
318  ),
319  [
320  'SKU' => $price->getSku(),
321  'websiteId' => $price->getWebsiteId(),
322  'customerGroup' => $price->getCustomerGroup(),
323  'qty' => $price->getQuantity()
324  ]
325  );
326  }
327  }
328 
337  private function checkWebsite(\Magento\Catalog\Api\Data\TierPriceInterface $price, $key, Result $validationResult)
338  {
339  try {
340  $this->websiteRepository->getById($price->getWebsiteId());
341  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
342  $validationResult->addFailedItem(
343  $key,
344  __(
345  'Invalid attribute Website ID = %websiteId. '
346  . 'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
347  [
348  'SKU' => '%SKU',
349  'websiteId' => '%websiteId',
350  'customerGroup' => '%customerGroup',
351  'qty' => '%qty'
352  ]
353  ),
354  [
355  'SKU' => $price->getSku(),
356  'websiteId' => $price->getWebsiteId(),
357  'customerGroup' => $price->getCustomerGroup(),
358  'qty' => $price->getQuantity()
359  ]
360  );
361  }
362  }
363 
373  private function checkUnique(
374  \Magento\Catalog\Api\Data\TierPriceInterface $tierPrice,
375  array $prices,
376  $key,
377  Result $validationResult
378  ) {
379  if (isset($prices[$tierPrice->getSku()])) {
380  foreach ($prices[$tierPrice->getSku()] as $price) {
381  if (strtolower($price->getCustomerGroup()) === strtolower($tierPrice->getCustomerGroup())
382  && $price->getQuantity() == $tierPrice->getQuantity()
383  && (
384  ($price->getWebsiteId() == $this->allWebsitesValue
385  || $tierPrice->getWebsiteId() == $this->allWebsitesValue)
386  && $price->getWebsiteId() != $tierPrice->getWebsiteId()
387  )
388  ) {
389  $validationResult->addFailedItem(
390  $key,
391  __(
392  'We found a duplicate website, tier price, customer group and quantity: '
393  . 'Customer Group = %customerGroup, Website ID = %websiteId, Quantity = %qty. '
394  . 'Row ID: SKU = %SKU, Website ID: %websiteId, '
395  . 'Customer Group: %customerGroup, Quantity: %qty.',
396  [
397  'SKU' => '%SKU',
398  'websiteId' => '%websiteId',
399  'customerGroup' => '%customerGroup',
400  'qty' => '%qty'
401  ]
402  ),
403  [
404  'SKU' => $price->getSku(),
405  'websiteId' => $price->getWebsiteId(),
406  'customerGroup' => $price->getCustomerGroup(),
407  'qty' => $price->getQuantity()
408  ]
409  );
410  }
411  }
412  }
413  }
414 
423  private function checkGroup(\Magento\Catalog\Api\Data\TierPriceInterface $price, $key, Result $validationResult)
424  {
425  $customerGroup = strtolower($price->getCustomerGroup());
426 
427  if ($customerGroup != $this->allGroupsValue && false === $this->retrieveGroupValue($customerGroup)) {
428  $validationResult->addFailedItem(
429  $key,
430  __(
431  'No such entity with Customer Group = %customerGroup. '
432  . 'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
433  [
434  'SKU' => '%SKU',
435  'websiteId' => '%websiteId',
436  'customerGroup' => '%customerGroup',
437  'qty' => '%qty'
438  ]
439  ),
440  [
441  'SKU' => $price->getSku(),
442  'websiteId' => $price->getWebsiteId(),
443  'customerGroup' => $price->getCustomerGroup(),
444  'qty' => $price->getQuantity()
445  ]
446  );
447  }
448  }
449 
456  private function retrieveGroupValue($code)
457  {
458  if (!isset($this->customerGroupsByCode[$code])) {
459  $searchCriteria = $this->searchCriteriaBuilder->addFilters(
460  [
461  $this->filterBuilder->setField('customer_group_code')->setValue($code)->create()
462  ]
463  );
464  $items = $this->customerGroupRepository->getList($searchCriteria->create())->getItems();
465  $item = array_shift($items);
466 
467  if (!$item) {
468  return false;
469  }
470 
471  $this->customerGroupsByCode[strtolower($item->getCode())] = $item->getId();
472  }
473 
474  return $this->customerGroupsByCode[$code];
475  }
476 }
__()
Definition: __.php:13
$price
foreach($websiteCodes as $websiteCode) $skus
$searchCriteria
addFailedItem($id, $message, array $parameters=[])
Definition: Result.php:48
$searchCriteriaBuilder
__construct(\Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Api\FilterBuilder $filterBuilder, \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository, \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository, \Magento\Catalog\Model\Product\Price\TierPricePersistence $tierPricePersistence, \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult, \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor, array $allowedProductTypes=[])
$code
Definition: info.phtml:12
$items