Skip to content

Instantly share code, notes, and snippets.

@adamzero1
Last active August 26, 2021 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamzero1/cef1cc84c80dae7c4fc8c349178bb639 to your computer and use it in GitHub Desktop.
Save adamzero1/cef1cc84c80dae7c4fc8c349178bb639 to your computer and use it in GitHub Desktop.
Magento doule decrementing stock when shipping orders
--- Model/GetItemsToDeductFromShipment.php 2020-11-26 10:23:56.827000000 +0000
+++ Model/GetItemsToDeductFromShipment.php 2020-11-26 10:22:50.741302876 +0000
@@ -17,6 +17,7 @@
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Sales\Model\Order\Shipment;
use Magento\Sales\Model\Order\Shipment\Item;
+use Magento\CatalogInventory\Api\StockConfigurationInterface;
/**
* Get source items for deduction class.
@@ -44,6 +45,11 @@
private $itemCouldBeDeducted;
/**
+ * @var StockConfigurationInterface
+ */
+ protected $stockConfiguration;
+
+ /**
* @param GetSkuFromOrderItemInterface $getSkuFromOrderItem
* @param Json $jsonSerializer
* @param ItemToDeductInterfaceFactory $itemToDeduct
@@ -53,6 +59,7 @@
GetSkuFromOrderItemInterface $getSkuFromOrderItem,
Json $jsonSerializer,
ItemToDeductInterfaceFactory $itemToDeduct,
+ StockConfigurationInterface $stockConfiguration,
IsItemCouldBeDeductedByTypes $itemCouldBeDeducted = null
) {
$this->jsonSerializer = $jsonSerializer;
@@ -60,6 +67,7 @@
$this->getSkuFromOrderItem = $getSkuFromOrderItem;
$this->itemCouldBeDeducted = $itemCouldBeDeducted ?: ObjectManager::getInstance()
->get(IsItemCouldBeDeductedByTypes::class);
+ $this->stockConfiguration = $stockConfiguration;
}
/**
@@ -73,6 +81,13 @@
{
$itemsToShip = [];
+ // if canSubtractQty === true
+ // this means "deduct when order is placed" so we
+ // don't want to deduct it again when shipping the order
+ if($this->stockConfiguration->canSubtractQty($shipment->getStoreId())){
+ return $itemsToShip;
+ }
+
/** @var \Magento\Sales\Model\Order\Shipment\Item $shipmentItem */
foreach ($shipment->getAllItems() as $shipmentItem) {
$orderItem = $shipmentItem->getOrderItem();
## this should be added to your composer.json. It should NOT replace your composer.json
"extra": {
"patches": {
"magento/module-inventory-shipping": {
"double decrementing stock issue": "patches/double_decrementing_stock_on_shipment.patch"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment