-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' into issue-26437-customer-shopping-cart-tab
- Loading branch information
Showing
40 changed files
with
1,151 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
app/code/Magento/CatalogImportExport/Model/Import/Product/StatusProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogImportExport\Model\Import\Product; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModelFactory; | ||
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
|
||
/** | ||
* Imported product status state manager | ||
*/ | ||
class StatusProcessor | ||
{ | ||
private const ATTRIBUTE_CODE = 'status'; | ||
/** | ||
* @var array | ||
*/ | ||
private $oldData; | ||
/** | ||
* @var array | ||
*/ | ||
private $newData; | ||
/** | ||
* @var ResourceModelFactory | ||
*/ | ||
private $resourceFactory; | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
/** | ||
* @var MetadataPool | ||
*/ | ||
private $metadataPool; | ||
/** | ||
* @var string | ||
*/ | ||
private $productEntityLinkField; | ||
/** | ||
* @var AbstractAttribute | ||
*/ | ||
private $attribute; | ||
|
||
/** | ||
* Initializes dependencies. | ||
* | ||
* @param MetadataPool $metadataPool | ||
* @param ResourceModelFactory $resourceFactory | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
MetadataPool $metadataPool, | ||
ResourceModelFactory $resourceFactory, | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->oldData = []; | ||
$this->newData = []; | ||
$this->resourceFactory = $resourceFactory; | ||
$this->resourceConnection = $resourceConnection; | ||
$this->metadataPool = $metadataPool; | ||
} | ||
|
||
/** | ||
* Check if status has changed for given (sku, storeId) | ||
* | ||
* @param string $sku | ||
* @param int $storeId | ||
* @return bool | ||
*/ | ||
public function isStatusChanged(string $sku, int $storeId): bool | ||
{ | ||
$sku = strtolower($sku); | ||
if (!isset($this->newData[$sku][$storeId])) { | ||
$changed = false; | ||
} elseif (!isset($this->oldData[$sku][$storeId])) { | ||
$changed = true; | ||
} else { | ||
$oldStatus = (int) $this->oldData[$sku][$storeId]; | ||
$newStatus = (int) $this->newData[$sku][$storeId]; | ||
$changed = $oldStatus !== $newStatus; | ||
} | ||
return $changed; | ||
} | ||
|
||
/** | ||
* Load old status data | ||
* | ||
* @param array $linkIdBySku | ||
*/ | ||
public function loadOldStatus(array $linkIdBySku): void | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$linkId = $this->getProductEntityLinkField(); | ||
$select = $connection->select() | ||
->from($this->getAttribute()->getBackend()->getTable()) | ||
->columns([$linkId, 'store_id', 'value']) | ||
->where(sprintf('%s IN (?)', $linkId), array_values($linkIdBySku)); | ||
$skuByLinkId = array_flip($linkIdBySku); | ||
|
||
foreach ($connection->fetchAll($select) as $item) { | ||
if (isset($skuByLinkId[$item[$linkId]])) { | ||
$this->oldData[$skuByLinkId[$item[$linkId]]][$item['store_id']] = $item['value']; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Set SKU status for given storeId | ||
* | ||
* @param string $sku | ||
* @param string $storeId | ||
* @param int $value | ||
*/ | ||
public function setStatus(string $sku, string $storeId, int $value): void | ||
{ | ||
$sku = strtolower($sku); | ||
$this->newData[$sku][$storeId] = $value; | ||
} | ||
|
||
/** | ||
* Get product entity link field. | ||
* | ||
* @return string | ||
*/ | ||
private function getProductEntityLinkField() | ||
{ | ||
if (!$this->productEntityLinkField) { | ||
$this->productEntityLinkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); | ||
} | ||
|
||
return $this->productEntityLinkField; | ||
} | ||
|
||
/** | ||
* Get Attribute model | ||
* | ||
* @return AbstractAttribute | ||
*/ | ||
private function getAttribute(): AbstractAttribute | ||
{ | ||
if ($this->attribute === null) { | ||
$this->attribute = $this->resourceFactory->create()->getAttribute(self::ATTRIBUTE_CODE); | ||
} | ||
return $this->attribute; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
app/code/Magento/CatalogImportExport/Model/Import/Product/StockProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogImportExport\Model\Import\Product; | ||
|
||
use Magento\Framework\Indexer\IndexerRegistry; | ||
|
||
/** | ||
* Imported product stock manager | ||
*/ | ||
class StockProcessor | ||
{ | ||
/** | ||
* @var IndexerRegistry | ||
*/ | ||
private $indexerRegistry; | ||
/** | ||
* @var array | ||
*/ | ||
private $indexers; | ||
|
||
/** | ||
* Initializes dependencies. | ||
* | ||
* @param IndexerRegistry $indexerRegistry | ||
* @param array $indexers | ||
*/ | ||
public function __construct( | ||
IndexerRegistry $indexerRegistry, | ||
array $indexers = [] | ||
) { | ||
$this->indexerRegistry = $indexerRegistry; | ||
$this->indexers = array_filter($indexers); | ||
} | ||
|
||
/** | ||
* Reindex products by ids | ||
* | ||
* @param array $ids | ||
* @return void | ||
*/ | ||
public function reindexList(array $ids = []): void | ||
{ | ||
if ($ids) { | ||
foreach ($this->indexers as $indexerName) { | ||
$indexer = $this->indexerRegistry->get($indexerName); | ||
if (!$indexer->isScheduled()) { | ||
$indexer->reindexList($ids); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.