I am creating a shipment programatically. When an order is placed for the product, at order completion, quantity per source gets reduced by 2 instead of 1.
Here's my code
if($this->order->hasShipments()){
return;
}
$shipment = $this->convertOrder->toShipment($this->order);
foreach($this->order->getAllItems() as $orderItem){
$qtyShipped = $orderItem->getQtyToShip() == 0 ? 1 : $orderItem->getQtyToShip() ;
$shipmentItem = $this->convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
$shipment->addItem($shipmentItem);
}
$shipment->register();
$shipment->getOrder()->setIsInProcess(true);
$websiteId = $order->getStore()->getWebsiteId();
$stockId = $this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
$sources = $this->getSourcesAssignedToStockOrderedByPriority->execute((int)$stockId);
foreach ($sources as $source) {
$sourceCode = $source->getSourceCode();
}
$shipmentExtension = $shipment->getExtensionAttributes();
if (empty($shipmentExtension)) {
$shipmentExtension = $this->shipmentExtensionFactory->create();
}
$shipmentExtension->setSourceCode($sourceCode);
$shipment->setExtensionAttributes($shipmentExtension);
$shipment->save();
Thanks!
inventory_reservation
table especially look for negative and positive numbers inquantity
column, because there might be rows with positivequantity
, which are compensating negative values.inventory_reservation
table. Record 1 has qty as -1.000 and metadata is-event_type: order_placed. The other 2 records have qty as 1.000 and metadata is - evet_type:shipment_created.