Skip to content

Commit

Permalink
Added support for stock updates via REST api
Browse files Browse the repository at this point in the history
  • Loading branch information
Reense committed Feb 2, 2024
1 parent c3a59b7 commit 56c0ed6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const STOCK_UPDATE = 1;
const ORDER_CANCEL = 2;
const CREDIT_MEMO = 3;
const WEBAPI_STOCK_UPDATE = 4;

const MOVEMENT_SECTION = "movement_section";
const NEW_PRODUCT = "new_product";
const MOVEMENT_DATA = "movement_data";
Expand Down
6 changes: 6 additions & 0 deletions Plugin/Stock/StockItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public function afterSave(
$message = __('Product restocked after order cancellation');
}
$this->movementRepository->insertStockMovement($stockItem, $message);
} elseif ($movementSection === InventoryLogHelper::WEBAPI_STOCK_UPDATE) {
$message = __('Stock updated via WebAPI');
$stockAutoFlag = $stockItem->getStockStatusChangedAutomaticallyFlag();
if (!$stockAutoFlag || $stockItem->getOldQty() != $stockItem->getQty() || $newProduct == 1) {
$this->movementRepository->insertStockMovement($stockItem, $message, $newProduct);
}
}
$this->helper->unRegisterAllData();
}
Expand Down
27 changes: 27 additions & 0 deletions Plugin/Webapi/Stock/StockItemRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Elgentos\InventoryLog\Plugin\Webapi\Stock;

class StockItemRepository
{
public function __construct(
public \Magento\Framework\Registry $registry,
public \Elgentos\InventoryLog\Helper\Data $helper
) {}

public function beforeSave(
$subject,
\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
): array {
if (!$this->helper->isModuleEnabled()) {
return [$stockItem];
}

$this->registry->register(
\Elgentos\InventoryLog\Helper\Data::MOVEMENT_SECTION,
\Elgentos\InventoryLog\Helper\Data::WEBAPI_STOCK_UPDATE
);

return [$stockItem];
}
}
2 changes: 1 addition & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogInventory\Model\Stock\StockItemRepository">
<plugin name="update_stockitem_ukey" type="Elgentos\InventoryLog\Plugin\Stock\StockItemRepository" sortOrder="1" disabled="false"/>
<plugin name="update_stockitem_ukey" type="Elgentos\InventoryLog\Plugin\Stock\StockItemRepository" sortOrder="2" disabled="false"/>
</type>

<preference for="Magento\CatalogImportExport\Model\Import\Product"
Expand Down
9 changes: 9 additions & 0 deletions etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogInventory\Model\Stock\StockItemRepository">
<plugin name="update_stockitem_via_webapi" type="Elgentos\InventoryLog\Plugin\Webapi\Stock\StockItemRepository"
sortOrder="1"
disabled="false"/>
</type>
</config>

0 comments on commit 56c0ed6

Please sign in to comment.