Skip to content

Commit

Permalink
Refactor of SetStockStatus and EnableManageStock plugins (#23)
Browse files Browse the repository at this point in the history
* Refactor syncing stock status based on backorder

* Remove obsolete repositories

* Replace values with constants

Co-authored-by: Robin Mulder <[email protected]>
  • Loading branch information
rbnmulder and Robin Mulder authored Sep 14, 2022
1 parent 14c56ca commit 16b32ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 70 deletions.
41 changes: 6 additions & 35 deletions Plugin/EnableManageStock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}
}
42 changes: 7 additions & 35 deletions Plugin/SetStockStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand All @@ -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);
}
}

0 comments on commit 16b32ba

Please sign in to comment.