Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BasePriceStorage.php
Go to the documentation of this file.
1 <?php
8 
13 {
19  private $attributeCode = 'price';
20 
24  private $pricePersistence;
25 
29  private $basePriceInterfaceFactory;
30 
34  private $productIdLocator;
35 
39  private $storeRepository;
40 
44  private $productRepository;
45 
49  private $validationResult;
50 
54  private $pricePersistenceFactory;
55 
59  private $invalidSkuProcessor;
60 
66  private $priceTypeAllowed = 1;
67 
73  private $allowedProductTypes = [];
74 
85  public function __construct(
86  PricePersistenceFactory $pricePersistenceFactory,
87  \Magento\Catalog\Api\Data\BasePriceInterfaceFactory $basePriceInterfaceFactory,
88  \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator,
89  \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
90  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
91  \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult,
92  \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor,
93  array $allowedProductTypes = []
94  ) {
95  $this->pricePersistenceFactory = $pricePersistenceFactory;
96  $this->basePriceInterfaceFactory = $basePriceInterfaceFactory;
97  $this->productIdLocator = $productIdLocator;
98  $this->storeRepository = $storeRepository;
99  $this->productRepository = $productRepository;
100  $this->validationResult = $validationResult;
101  $this->allowedProductTypes = $allowedProductTypes;
102  $this->invalidSkuProcessor = $invalidSkuProcessor;
103  }
104 
108  public function get(array $skus)
109  {
110  $skus = $this->invalidSkuProcessor->filterSkuList(
111  $skus,
112  $this->allowedProductTypes,
113  $this->priceTypeAllowed
114  );
115  $rawPrices = $this->getPricePersistence()->get($skus);
116  $prices = [];
117  foreach ($rawPrices as $rawPrice) {
118  $price = $this->basePriceInterfaceFactory->create();
119  $sku = $this->getPricePersistence()
120  ->retrieveSkuById($rawPrice[$this->getPricePersistence()->getEntityLinkField()], $skus);
121  $price->setSku($sku);
122  $price->setPrice($rawPrice['value']);
123  $price->setStoreId($rawPrice['store_id']);
124  $prices[] = $price;
125  }
126 
127  return $prices;
128  }
129 
133  public function update(array $prices)
134  {
135  $prices = $this->retrieveValidPrices($prices);
136  $formattedPrices = [];
137 
138  foreach ($prices as $price) {
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(),
145  ];
146  }
147  }
148 
149  $this->getPricePersistence()->update($formattedPrices);
150 
151  return $this->validationResult->getFailedItems();
152  }
153 
159  private function getPricePersistence()
160  {
161  if (!$this->pricePersistence) {
162  $this->pricePersistence = $this->pricePersistenceFactory->create(['attributeCode' => $this->attributeCode]);
163  }
164 
165  return $this->pricePersistence;
166  }
167 
174  private function retrieveValidPrices(array $prices)
175  {
176  $skus = array_unique(
177  array_map(function ($price) {
178  return $price->getSku();
179  }, $prices)
180  );
181  $invalidSkus = $this->invalidSkuProcessor->retrieveInvalidSkuList(
182  $skus,
183  $this->allowedProductTypes,
184  $this->priceTypeAllowed
185  );
186 
187  foreach ($prices as $id => $price) {
188  if (!$price->getSku() || in_array($price->getSku(), $invalidSkus)) {
189  $this->validationResult->addFailedItem(
190  $id,
191  __(
192  'Invalid attribute %fieldName = %fieldValue.',
193  ['fieldName' => '%fieldName', 'fieldValue' => '%fieldValue']
194  ),
195  ['fieldName' => 'SKU', 'fieldValue' => $price->getSku()]
196  );
197  }
198  if (null === $price->getPrice() || $price->getPrice() < 0) {
199  $this->validationResult->addFailedItem(
200  $id,
201  __(
202  'Invalid attribute %fieldName = %fieldValue.',
203  ['fieldName' => '%fieldName', 'fieldValue' => '%fieldValue']
204  ),
205  ['fieldName' => 'Price', 'fieldValue' => $price->getPrice()]
206  );
207  }
208  try {
209  $this->storeRepository->getById($price->getStoreId());
210  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
211  $this->validationResult->addFailedItem(
212  $id,
213  __(
214  'Requested store is not found. Row ID: SKU = %SKU, Store ID: %storeId.',
215  ['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
216  ),
217  ['SKU' => $price->getSku(), 'storeId' => $price->getStoreId()]
218  );
219  }
220  }
221 
222  foreach ($this->validationResult->getFailedRowIds() as $id) {
223  unset($prices[$id]);
224  }
225 
226  return $prices;
227  }
228 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$price
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=[])