17 private $productIdLocator;
22 private $searchCriteriaBuilder;
27 private $filterBuilder;
32 private $customerGroupRepository;
37 private $websiteRepository;
42 private $validationResult;
47 private $tierPricePersistence;
54 private $customerGroupsByCode = [];
59 private $invalidSkuProcessor;
66 private $allGroupsValue =
'all groups';
73 private $allWebsitesValue =
"0";
80 private $allowedProductTypes = [];
97 \
Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
100 \
Magento\
Store\Api\WebsiteRepositoryInterface $websiteRepository,
104 array $allowedProductTypes = []
106 $this->productIdLocator = $productIdLocator;
108 $this->filterBuilder = $filterBuilder;
109 $this->customerGroupRepository = $customerGroupRepository;
111 $this->tierPricePersistence = $tierPricePersistence;
112 $this->validationResult = $validationResult;
113 $this->invalidSkuProcessor = $invalidSkuProcessor;
114 $this->allowedProductTypes = $allowedProductTypes;
125 return $this->invalidSkuProcessor->filterSkuList(
$skus, $this->allowedProductTypes);
137 $validationResult = clone $this->validationResult;
138 $skus = array_unique(
139 array_map(
function (
$price) {
143 $skuDiff = $this->invalidSkuProcessor->retrieveInvalidSkuList(
$skus, $this->allowedProductTypes);
144 $idsBySku = $this->productIdLocator->retrieveProductIdsBySkus(
$skus);
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);
162 $this->checkUnique(
$price, $existingPrices, $key, $validationResult);
163 $this->checkGroup(
$price, $key, $validationResult);
166 return $validationResult;
178 private function checkSku(
184 if (!
$price->getSku() || in_array(
$price->getSku(), $invalidSkus)) {
188 'Invalid attribute SKU = %SKU. ' 189 .
'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
192 'websiteId' =>
'%websiteId',
193 'customerGroup' =>
'%customerGroup',
198 'SKU' =>
$price->getSku(),
199 'websiteId' =>
$price->getWebsiteId(),
200 'customerGroup' =>
$price->getCustomerGroup(),
201 'qty' =>
$price->getQuantity()
215 private function checkPrice(\
Magento\Catalog\Api\Data\TierPriceInterface
$price, $key, Result $validationResult)
217 if (
null ===
$price->getPrice()
220 &&
$price->getPrice() > 100
223 $validationResult->addFailedItem(
226 'Invalid attribute Price = %price. ' 227 .
'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
231 'websiteId' =>
'%websiteId',
232 'customerGroup' =>
'%customerGroup',
237 'price' =>
$price->getPrice(),
238 'SKU' =>
$price->getSku(),
239 'websiteId' =>
$price->getWebsiteId(),
240 'customerGroup' =>
$price->getCustomerGroup(),
241 'qty' =>
$price->getQuantity()
256 private function checkPriceType(
260 Result $validationResult
272 $validationResult->addFailedItem(
275 'Invalid attribute Price Type = %priceType. ' 276 .
'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
280 'websiteId' =>
'%websiteId',
281 'customerGroup' =>
'%customerGroup',
286 'priceType' =>
$price->getPriceType(),
287 'SKU' =>
$price->getSku(),
288 'websiteId' =>
$price->getWebsiteId(),
289 'customerGroup' =>
$price->getCustomerGroup(),
290 'qty' =>
$price->getQuantity()
304 private function checkQuantity(\
Magento\Catalog\Api\Data\TierPriceInterface
$price, $key, Result $validationResult)
306 if (
$price->getQuantity() < 1) {
307 $validationResult->addFailedItem(
310 'Invalid attribute Quantity = %qty. ' 311 .
'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
314 'websiteId' =>
'%websiteId',
315 'customerGroup' =>
'%customerGroup',
320 'SKU' =>
$price->getSku(),
321 'websiteId' =>
$price->getWebsiteId(),
322 'customerGroup' =>
$price->getCustomerGroup(),
323 'qty' =>
$price->getQuantity()
337 private function checkWebsite(\
Magento\Catalog\Api\Data\TierPriceInterface
$price, $key, Result $validationResult)
340 $this->websiteRepository->getById(
$price->getWebsiteId());
341 }
catch (\
Magento\Framework\Exception\NoSuchEntityException $e) {
342 $validationResult->addFailedItem(
345 'Invalid attribute Website ID = %websiteId. ' 346 .
'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
349 'websiteId' =>
'%websiteId',
350 'customerGroup' =>
'%customerGroup',
355 'SKU' =>
$price->getSku(),
356 'websiteId' =>
$price->getWebsiteId(),
357 'customerGroup' =>
$price->getCustomerGroup(),
358 'qty' =>
$price->getQuantity()
373 private function checkUnique(
374 \
Magento\Catalog\Api\Data\TierPriceInterface $tierPrice,
377 Result $validationResult
379 if (isset(
$prices[$tierPrice->getSku()])) {
381 if (strtolower(
$price->getCustomerGroup()) === strtolower($tierPrice->getCustomerGroup())
382 &&
$price->getQuantity() == $tierPrice->getQuantity()
384 (
$price->getWebsiteId() == $this->allWebsitesValue
385 || $tierPrice->getWebsiteId() == $this->allWebsitesValue)
386 &&
$price->getWebsiteId() != $tierPrice->getWebsiteId()
389 $validationResult->addFailedItem(
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.',
398 'websiteId' =>
'%websiteId',
399 'customerGroup' =>
'%customerGroup',
404 'SKU' =>
$price->getSku(),
405 'websiteId' =>
$price->getWebsiteId(),
406 'customerGroup' =>
$price->getCustomerGroup(),
407 'qty' =>
$price->getQuantity()
423 private function checkGroup(\
Magento\Catalog\Api\Data\TierPriceInterface
$price, $key, Result $validationResult)
425 $customerGroup = strtolower(
$price->getCustomerGroup());
427 if ($customerGroup != $this->allGroupsValue &&
false === $this->retrieveGroupValue($customerGroup)) {
428 $validationResult->addFailedItem(
431 'No such entity with Customer Group = %customerGroup. ' 432 .
'Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.',
435 'websiteId' =>
'%websiteId',
436 'customerGroup' =>
'%customerGroup',
441 'SKU' =>
$price->getSku(),
442 'websiteId' =>
$price->getWebsiteId(),
443 'customerGroup' =>
$price->getCustomerGroup(),
444 'qty' =>
$price->getQuantity()
456 private function retrieveGroupValue(
$code)
458 if (!isset($this->customerGroupsByCode[
$code])) {
461 $this->filterBuilder->setField(
'customer_group_code')->setValue(
$code)->create()
471 $this->customerGroupsByCode[strtolower(
$item->getCode())] =
$item->getId();
474 return $this->customerGroupsByCode[
$code];
const PRICE_TYPE_DISCOUNT
foreach($websiteCodes as $websiteCode) $skus
retrieveValidationResult(array $prices, array $existingPrices=[])
addFailedItem($id, $message, array $parameters=[])
validateSkus(array $skus)
__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=[])