19 private $specialPriceResource;
24 private $specialPriceFactory;
29 private $productIdLocator;
34 private $storeRepository;
39 private $validationResult;
44 private $invalidSkuProcessor;
49 private $allowedProductTypes = [];
62 \
Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory $specialPriceFactory,
64 \
Magento\
Store\Api\StoreRepositoryInterface $storeRepository,
67 array $allowedProductTypes = []
69 $this->specialPriceResource = $specialPriceResource;
70 $this->specialPriceFactory = $specialPriceFactory;
71 $this->productIdLocator = $productIdLocator;
73 $this->validationResult = $validationResult;
74 $this->invalidSkuProcessor = $invalidSkuProcessor;
75 $this->allowedProductTypes = $allowedProductTypes;
81 public function get(array
$skus)
83 $skus = $this->invalidSkuProcessor->filterSkuList(
$skus, $this->allowedProductTypes);
84 $rawPrices = $this->specialPriceResource->get(
$skus);
87 foreach ($rawPrices as $rawPrice) {
89 $price = $this->specialPriceFactory->create();
90 $sku = isset($rawPrice[
'sku'])
92 : $this->retrieveSkuById($rawPrice[$this->specialPriceResource->getEntityLinkField()],
$skus);
94 $price->setPrice($rawPrice[
'value']);
95 $price->setStoreId($rawPrice[
'store_id']);
96 $price->setPriceFrom($rawPrice[
'price_from']);
97 $price->setPriceTo($rawPrice[
'price_to']);
110 $this->specialPriceResource->update(
$prices);
112 return $this->validationResult->getFailedItems();
121 $this->specialPriceResource->delete(
$prices);
123 return $this->validationResult->getFailedItems();
132 private function retrieveValidPrices(array
$prices)
134 $skus = array_unique(
135 array_map(
function (
$price) {
139 $failedSkus = $this->invalidSkuProcessor->retrieveInvalidSkuList(
$skus, $this->allowedProductTypes);
142 if (!
$price->getSku() || in_array(
$price->getSku(), $failedSkus)) {
143 $this->validationResult->addFailedItem(
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.',
149 'SKU' =>
$price->getSku(),
150 'storeId' =>
$price->getStoreId(),
151 'priceFrom' =>
$price->getPriceFrom(),
152 'priceTo' =>
$price->getPriceTo()
156 'SKU' =>
$price->getSku(),
157 'storeId' =>
$price->getStoreId(),
158 'priceFrom' =>
$price->getPriceFrom(),
159 'priceTo' =>
$price->getPriceTo()
163 $this->checkPrice(
$price, $key);
164 $this->checkDate(
$price,
$price->getPriceFrom(),
'Price From', $key);
165 $this->checkDate(
$price,
$price->getPriceTo(),
'Price To', $key);
167 $this->storeRepository->getById(
$price->getStoreId());
168 }
catch (NoSuchEntityException $e) {
169 $this->validationResult->addFailedItem(
172 'Requested store is not found. ' 173 .
'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
175 'SKU' =>
$price->getSku(),
176 'storeId' =>
$price->getStoreId(),
177 'priceFrom' =>
$price->getPriceFrom(),
178 'priceTo' =>
$price->getPriceTo()
182 'SKU' =>
$price->getSku(),
183 'storeId' =>
$price->getStoreId(),
184 'priceFrom' =>
$price->getPriceFrom(),
185 'priceTo' =>
$price->getPriceTo()
191 foreach ($this->validationResult->getFailedRowIds() as
$id) {
210 $this->validationResult->addFailedItem(
213 'Invalid attribute %label = %priceTo. ' 214 .
'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
217 'SKU' =>
$price->getSku(),
218 'storeId' =>
$price->getStoreId(),
219 'priceFrom' =>
$price->getPriceFrom(),
220 'priceTo' =>
$price->getPriceTo()
225 'SKU' =>
$price->getSku(),
226 'storeId' =>
$price->getStoreId(),
227 'priceFrom' =>
$price->getPriceFrom(),
228 'priceTo' =>
$price->getPriceTo()
242 private function checkPrice(\
Magento\Catalog\Api\Data\SpecialPriceInterface
$price, $key)
244 if (
null ===
$price->getPrice() ||
$price->getPrice() < 0) {
245 $this->validationResult->addFailedItem(
248 'Invalid attribute Price = %price. ' 249 .
'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
251 'price' =>
$price->getPrice(),
252 'SKU' =>
$price->getSku(),
253 'storeId' =>
$price->getStoreId(),
254 'priceFrom' =>
$price->getPriceFrom(),
255 'priceTo' =>
$price->getPriceTo()
259 'price' =>
$price->getPrice(),
260 'SKU' =>
$price->getSku(),
261 'storeId' =>
$price->getStoreId(),
262 'priceFrom' =>
$price->getPriceFrom(),
263 'priceTo' =>
$price->getPriceTo()
278 foreach ($this->productIdLocator->retrieveProductIdsBySkus(
$skus) as $sku => $ids) {
293 private function isCorrectDateValue($date)
295 $actualDate = date(
'Y-m-d H:i:s', strtotime($date));
296 return $actualDate && $actualDate === $date;
foreach($websiteCodes as $websiteCode) $skus
__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=[])