From 16b32ba73a28fc7416811cecf5e48527a0afcad1 Mon Sep 17 00:00:00 2001 From: Robin Mulder Date: Wed, 14 Sep 2022 12:06:08 +0200 Subject: [PATCH] Refactor of SetStockStatus and EnableManageStock plugins (#23) * Refactor syncing stock status based on backorder * Remove obsolete repositories * Replace values with constants Co-authored-by: Robin Mulder --- Plugin/EnableManageStock.php | 41 ++++++----------------------------- Plugin/SetStockStatus.php | 42 ++++++------------------------------ 2 files changed, 13 insertions(+), 70 deletions(-) diff --git a/Plugin/EnableManageStock.php b/Plugin/EnableManageStock.php index c70a00c..8e3c6da 100644 --- a/Plugin/EnableManageStock.php +++ b/Plugin/EnableManageStock.php @@ -4,8 +4,6 @@ use Akeneo\Connector\Helper\Import\Product as ProductImportHelper; use Akeneo\Connector\Job\Product as Subject; -use Magento\Catalog\Model\ProductRepository; -use Magento\CatalogInventory\Model\Stock\StockItemRepository; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Store\Model\ScopeInterface as Scope; @@ -18,24 +16,14 @@ class EnableManageStock /** @var AdapterInterface */ protected $connection; - /** @var StockItemRepository */ - protected $stockItemRepository; - - /** @var ProductRepository */ - protected $productRepository; - /** @var ScopeConfigInterface */ protected $config; public function __construct( ProductImportHelper $entitiesHelper, - StockItemRepository $stockItemRepository, - ProductRepository $productRepository, ScopeConfigInterface $config ) { $this->entitiesHelper = $entitiesHelper; - $this->stockItemRepository = $stockItemRepository; - $this->productRepository = $productRepository; $this->config = $config; $this->connection = $this->entitiesHelper->getConnection(); @@ -50,39 +38,22 @@ public function afterInitStock(Subject $subject): bool } $products = $this->getProducts($subject); - - foreach ($products as $product) { - try { - $this->updateStockInfo($product); - } catch (\Exception $e) { - continue; - } + if(!empty($products)) { + $connection = $this->entitiesHelper->getConnection(); + $where = ['product_id' . ' IN(?)' => [$products]]; + $connection->update($this->entitiesHelper->getTable('cataloginventory_stock_item'), ['manage_stock' => '1','use_config_manage_stock' => '1'], $where); } - return true; } - protected function updateStockInfo(array $product): void - { - $product = $this->productRepository->get($product['identifier']); - - $stock = $product->getExtensionAttributes()->getStockItem(); - - $stock - ->setManageStock(1) - ->setUseConfigManageStock(1); - - $this->stockItemRepository->save($stock); - } - protected function getProducts(Subject $subject): array { $tmpTableName = $this->entitiesHelper->getTableName($subject->getCode()); - $query = $this->connection->select()->from(['t' => $tmpTableName])->joinInner( + $query = $this->connection->select()->from(['t' => $tmpTableName],['c.entity_id'])->joinInner( ['c' => 'catalog_product_entity'], 't.identifier = c.sku' ); - return $this->connection->fetchAll($query); + return $this->connection->fetchPairs($query); } } diff --git a/Plugin/SetStockStatus.php b/Plugin/SetStockStatus.php index 41e37ac..6d59c79 100644 --- a/Plugin/SetStockStatus.php +++ b/Plugin/SetStockStatus.php @@ -4,11 +4,10 @@ use Akeneo\Connector\Helper\Import\Product as ProductImportHelper; use Akeneo\Connector\Job\Product as Subject; -use Magento\Catalog\Model\ProductRepository; -use Magento\CatalogInventory\Model\Stock\StockItemRepository; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Store\Model\ScopeInterface as Scope; +use Magento\CatalogInventory\Model\Stock; class SetStockStatus { @@ -18,24 +17,14 @@ class SetStockStatus /** @var AdapterInterface */ protected $connection; - /** @var StockItemRepository */ - protected $stockItemRepository; - - /** @var ProductRepository */ - protected $productRepository; - /** @var ScopeConfigInterface */ protected $config; public function __construct( ProductImportHelper $entitiesHelper, - StockItemRepository $stockItemRepository, - ProductRepository $productRepository, ScopeConfigInterface $config ) { $this->entitiesHelper = $entitiesHelper; - $this->stockItemRepository = $stockItemRepository; - $this->productRepository = $productRepository; $this->config = $config; $this->connection = $this->entitiesHelper->getConnection(); @@ -50,39 +39,22 @@ public function afterInitStock(Subject $subject): bool } $products = $this->getProducts($subject); - - foreach ($products as $product) { - try { - $this->updateStockInfo($product); - } catch (\Exception $e) { - continue; - } + if(!empty($products)) { + $connection = $this->entitiesHelper->getConnection(); + $where = ['product_id' . ' IN(?)' => [$products], 'backorders' . ' IN(?)' => [Stock::BACKORDERS_YES_NONOTIFY, Stock::BACKORDERS_YES_NOTIFY]]; + $connection->update($this->entitiesHelper->getTable('cataloginventory_stock_item'), ['is_in_stock' => Stock::STOCK_IN_STOCK], $where); } - return true; } - protected function updateStockInfo(array $product): void - { - $product = $this->productRepository->get($product['identifier']); - - $stock = $product->getExtensionAttributes()->getStockItem(); - - if ($stock->getBackorders() === 1 || $stock->getBackorders() === 2) { - $stock->setIsInStock(1); - - $this->stockItemRepository->save($stock); - } - } - protected function getProducts(Subject $subject): array { $tmpTableName = $this->entitiesHelper->getTableName($subject->getCode()); - $query = $this->connection->select()->from(['t' => $tmpTableName])->joinInner( + $query = $this->connection->select()->from(['t' => $tmpTableName],['c.entity_id'])->joinInner( ['c' => 'catalog_product_entity'], 't.identifier = c.sku' ); - return $this->connection->fetchAll($query); + return $this->connection->fetchPairs($query); } }