0

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!

5
  • 1
    Reservation is created when the Order is created, not Shipping. Check if this is an issue when you remove code above. Check also what you have in your inventory_reservation table especially look for negative and positive numbers in quantity column, because there might be rows with positive quantity, which are compensating negative values.
    – dudzio
    Commented Jun 10, 2019 at 16:47
  • I do have both positive and negative quantities in the inventory_reservation table. Should it contain any positive values?
    – coderGeek
    Commented Jun 10, 2019 at 16:51
  • For every order, 3 records are being created in 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.
    – coderGeek
    Commented Jun 10, 2019 at 16:57
  • @coderGeek my inventory_reservation table looks the same as yours, with 2 of "event_type":"shipment_created" records for every "event_type":"order_placed" record. I don't think this is the correct behavior and my stock gets messed up. Can you please share what your solution to this problem was, if you found any? Thank you! Commented Sep 14, 2019 at 7:49
  • @RobertSchmidt Turns out I was saving the shipment twice which was creating the issue. Hope that helps
    – coderGeek
    Commented Sep 16, 2019 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.