19 private $attributeCode =
'price';
24 private $pricePersistence;
29 private $basePriceInterfaceFactory;
34 private $productIdLocator;
39 private $storeRepository;
44 private $productRepository;
49 private $validationResult;
54 private $pricePersistenceFactory;
59 private $invalidSkuProcessor;
66 private $priceTypeAllowed = 1;
73 private $allowedProductTypes = [];
86 PricePersistenceFactory $pricePersistenceFactory,
87 \
Magento\Catalog\Api\Data\BasePriceInterfaceFactory $basePriceInterfaceFactory,
89 \
Magento\
Store\Api\StoreRepositoryInterface $storeRepository,
93 array $allowedProductTypes = []
95 $this->pricePersistenceFactory = $pricePersistenceFactory;
96 $this->basePriceInterfaceFactory = $basePriceInterfaceFactory;
97 $this->productIdLocator = $productIdLocator;
100 $this->validationResult = $validationResult;
101 $this->allowedProductTypes = $allowedProductTypes;
102 $this->invalidSkuProcessor = $invalidSkuProcessor;
110 $skus = $this->invalidSkuProcessor->filterSkuList(
112 $this->allowedProductTypes,
113 $this->priceTypeAllowed
115 $rawPrices = $this->getPricePersistence()->get(
$skus);
117 foreach ($rawPrices as $rawPrice) {
118 $price = $this->basePriceInterfaceFactory->create();
119 $sku = $this->getPricePersistence()
120 ->retrieveSkuById($rawPrice[$this->getPricePersistence()->getEntityLinkField()],
$skus);
122 $price->setPrice($rawPrice[
'value']);
123 $price->setStoreId($rawPrice[
'store_id']);
136 $formattedPrices = [];
139 $ids = array_keys($this->productIdLocator->retrieveProductIdsBySkus([
$price->getSku()])[
$price->getSku()]);
140 foreach ($ids as
$id) {
141 $formattedPrices[] = [
142 'store_id' =>
$price->getStoreId(),
143 $this->getPricePersistence()->getEntityLinkField() =>
$id,
144 'value' =>
$price->getPrice(),
149 $this->getPricePersistence()->update($formattedPrices);
151 return $this->validationResult->getFailedItems();
159 private function getPricePersistence()
161 if (!$this->pricePersistence) {
162 $this->pricePersistence = $this->pricePersistenceFactory->create([
'attributeCode' => $this->attributeCode]);
165 return $this->pricePersistence;
174 private function retrieveValidPrices(array
$prices)
176 $skus = array_unique(
177 array_map(
function (
$price) {
181 $invalidSkus = $this->invalidSkuProcessor->retrieveInvalidSkuList(
183 $this->allowedProductTypes,
184 $this->priceTypeAllowed
188 if (!
$price->getSku() || in_array(
$price->getSku(), $invalidSkus)) {
189 $this->validationResult->addFailedItem(
192 'Invalid attribute %fieldName = %fieldValue.',
193 [
'fieldName' =>
'%fieldName',
'fieldValue' =>
'%fieldValue']
195 [
'fieldName' =>
'SKU',
'fieldValue' =>
$price->getSku()]
198 if (
null ===
$price->getPrice() ||
$price->getPrice() < 0) {
199 $this->validationResult->addFailedItem(
202 'Invalid attribute %fieldName = %fieldValue.',
203 [
'fieldName' =>
'%fieldName',
'fieldValue' =>
'%fieldValue']
205 [
'fieldName' =>
'Price',
'fieldValue' =>
$price->getPrice()]
209 $this->storeRepository->getById(
$price->getStoreId());
210 }
catch (\
Magento\Framework\Exception\NoSuchEntityException $e) {
211 $this->validationResult->addFailedItem(
214 'Requested store is not found. Row ID: SKU = %SKU, Store ID: %storeId.',
215 [
'SKU' =>
$price->getSku(),
'storeId' =>
$price->getStoreId()]
217 [
'SKU' =>
$price->getSku(),
'storeId' =>
$price->getStoreId()]
222 foreach ($this->validationResult->getFailedRowIds() as
$id) {
foreach($websiteCodes as $websiteCode) $skus
__construct(PricePersistenceFactory $pricePersistenceFactory, \Magento\Catalog\Api\Data\BasePriceInterfaceFactory $basePriceInterfaceFactory, \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator, \Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult, \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor, array $allowedProductTypes=[])