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!

  • 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 Jun 10 '19 at 16:47
  • I do have both positive and negative quantities in the inventory_reservation table. Should it contain any positive values? – coderGeek Jun 10 '19 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 Jun 10 '19 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! – Robert Schmidt Sep 14 '19 at 7:49
  • @RobertSchmidt Turns out I was saving the shipment twice which was creating the issue. Hope that helps – coderGeek Sep 16 '19 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.