19 private $attributeCode =
'cost';
24 private $pricePersistence;
29 private $costInterfaceFactory;
34 private $productIdLocator;
39 private $validationResult;
44 private $invalidSkuProcessor;
51 private $allowedProductTypes = [];
56 private $pricePersistenceFactory;
61 private $storeRepository;
75 PricePersistenceFactory $pricePersistenceFactory,
76 \
Magento\Catalog\Api\Data\CostInterfaceFactory $costInterfaceFactory,
78 \
Magento\
Store\Api\StoreRepositoryInterface $storeRepository,
81 array $allowedProductTypes = []
83 $this->pricePersistenceFactory = $pricePersistenceFactory;
84 $this->costInterfaceFactory = $costInterfaceFactory;
85 $this->productIdLocator = $productIdLocator;
87 $this->validationResult = $validationResult;
88 $this->invalidSkuProcessor = $invalidSkuProcessor;
89 $this->allowedProductTypes = $allowedProductTypes;
95 public function get(array
$skus)
97 $skus = $this->invalidSkuProcessor->filterSkuList(
$skus, $this->allowedProductTypes);
98 $rawPrices = $this->getPricePersistence()->get(
$skus);
100 foreach ($rawPrices as $rawPrice) {
101 $price = $this->costInterfaceFactory->create();
102 $sku = $this->getPricePersistence()
103 ->retrieveSkuById($rawPrice[$this->getPricePersistence()->getEntityLinkField()],
$skus);
105 $price->setCost($rawPrice[
'value']);
106 $price->setStoreId($rawPrice[
'store_id']);
119 $formattedPrices = [];
122 $productIdsBySkus = $this->productIdLocator->retrieveProductIdsBySkus([
$price->getSku()]);
125 $formattedPrices[] = [
126 'store_id' =>
$price->getStoreId(),
127 $this->getPricePersistence()->getEntityLinkField() =>
$id,
128 'value' =>
$price->getCost(),
133 $this->getPricePersistence()->update($formattedPrices);
135 return $this->validationResult->getFailedItems();
143 $skus = $this->invalidSkuProcessor->filterSkuList(
$skus, $this->allowedProductTypes);
144 $this->getPricePersistence()->delete(
$skus);
154 private function getPricePersistence()
156 if (!$this->pricePersistence) {
157 $this->pricePersistence = $this->pricePersistenceFactory->create([
'attributeCode' => $this->attributeCode]);
160 return $this->pricePersistence;
169 private function retrieveValidPrices(array
$prices)
171 $skus = array_unique(
172 array_map(
function (
$price) {
176 $invalidSkus = $this->invalidSkuProcessor->retrieveInvalidSkuList(
$skus, $this->allowedProductTypes);
179 if (!
$price->getSku() || in_array(
$price->getSku(), $invalidSkus)) {
180 $this->validationResult->addFailedItem(
183 'Invalid attribute %fieldName = %fieldValue.',
184 [
'fieldName' =>
'%fieldName',
'fieldValue' =>
'%fieldValue']
186 [
'fieldName' =>
'SKU',
'fieldValue' =>
$price->getSku()]
189 if (
null ===
$price->getCost() ||
$price->getCost() < 0) {
190 $this->validationResult->addFailedItem(
193 'Invalid attribute Cost = %cost. Row ID: SKU = %SKU, Store ID: %storeId.',
194 [
'cost' =>
$price->getCost(),
'SKU' =>
$price->getSku(),
'storeId' =>
$price->getStoreId()]
196 [
'cost' =>
$price->getCost(),
'SKU' =>
$price->getSku(),
'storeId' =>
$price->getStoreId()]
200 $this->storeRepository->getById(
$price->getStoreId());
201 }
catch (\
Magento\Framework\Exception\NoSuchEntityException $e) {
202 $this->validationResult->addFailedItem(
205 'Requested store is not found. Row ID: SKU = %SKU, Store ID: %storeId.',
206 [
'SKU' =>
$price->getSku(),
'storeId' =>
$price->getStoreId()]
208 [
'SKU' =>
$price->getSku(),
'storeId' =>
$price->getStoreId()]
213 foreach ($this->validationResult->getFailedRowIds() as
$id) {
foreach($websiteCodes as $websiteCode) $skus
__construct(PricePersistenceFactory $pricePersistenceFactory, \Magento\Catalog\Api\Data\CostInterfaceFactory $costInterfaceFactory, \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=[])