inventory icon indicating copy to clipboard operation
inventory copied to clipboard

Inventory Reserveration order_placed

Open pmathbliss opened this issue 1 year ago • 4 comments

Preconditions (*)

  1. version 2.4.3 - 2.4.6

Steps to reproduce (*)

  1. Create any order.

Expected result (*)

  1. The object_id is populated in inventory_reservation.meta_key

  2. {"event_type":"order_placed","object_type":"order","object_id":"14","object_increment_id":"000000022"}

  3. I am currently on version 2.4.3 in my development but the object_id is missing on a 2.4.6. The idea is that populating this id would make the processing of the inventory reservations and cleaning up missing reservations much quicker. I also have a request the inventory reservation and testing quantity available is done in one transaction. I've attached the full log sql of creating test orders.

  4. The above example was produced by moving the sale event to after the Proceed. $order = $proceed($order);

  5. Here is my update to AppendReservationsAfterOrderPlacementPlugin: \var\www\html\vendor\magento\module-inventory-sales\Plugin\Sales\OrderManagement\AppendReservationsAfterOrderPlacementPlugin.php `public function aroundPlace( OrderManagementInterface $subject, callable $proceed, OrderInterface $order ): OrderInterface { $itemsById = $itemsBySku = $itemsToSell = []; foreach ($order->getItems() as $item) { if (!isset($itemsById[$item->getProductId()])) { $itemsById[$item->getProductId()] = 0; } $itemsById[$item->getProductId()] += $item->getQtyOrdered(); } $productSkus = $this->getSkusByProductIds->execute(array_keys($itemsById)); $productTypes = $this->getProductTypesBySkus->execute($productSkus);

     foreach ($productSkus as $productId => $sku) {
         if (false === $this->isSourceItemManagementAllowedForProductType->execute($productTypes[$sku])) {
             continue;
         }
    
         $itemsBySku[$sku] = (float)$itemsById[$productId];
         $itemsToSell[] = $this->itemsToSellFactory->create([
             'sku' => $sku,
             'qty' => -(float)$itemsById[$productId]
         ]);
     }
    
     $websiteId = (int)$order->getStore()->getWebsiteId();
     $websiteCode = $this->websiteRepository->getById($websiteId)->getCode();
     $stockId = (int)$this->stockByWebsiteIdResolver->execute((int)$websiteId)->getStockId();
    
     $this->checkItemsQuantity->execute($itemsBySku, $stockId);
    
     /** @var SalesEventExtensionInterface */
     $salesEventExtension = $this->salesEventExtensionFactory->create([
         'data' => ['objectIncrementId' => (string)$order->getIncrementId()]
     ]);
    
    
    
     try {
         $order = $proceed($order);
         /** @var SalesEventInterface $salesEvent */
     $salesEvent = $this->salesEventFactory->create([
         'type' => SalesEventInterface::EVENT_ORDER_PLACED,
         'objectType' => SalesEventInterface::OBJECT_TYPE_ORDER,
         'objectId' => (string)$order->getEntityId()
     ]);
     $salesEvent->setExtensionAttributes($salesEventExtension);
     $salesChannel = $this->salesChannelFactory->create([
         'data' => [
             'type' => SalesChannelInterface::TYPE_WEBSITE,
             'code' => $websiteCode
         ]
     ]);
    
     $this->placeReservationsForSalesEvent->execute($itemsToSell, $salesChannel, $salesEvent);
    
     } catch (\Exception $e) {
         //add compensation
         foreach ($itemsToSell as $item) {
             $item->setQuantity(-(float)$item->getQuantity());
         }
    
         /** @var SalesEventInterface $salesEvent */
         $salesEvent = $this->salesEventFactory->create([
             'type' => SalesEventInterface::EVENT_ORDER_PLACE_FAILED,
             'objectType' => SalesEventInterface::OBJECT_TYPE_ORDER,
             'objectId' => (string)$order->getEntityId()
         ]);
         $salesEvent->setExtensionAttributes($salesEventExtension);
    
         $this->placeReservationsForSalesEvent->execute($itemsToSell, $salesChannel, $salesEvent);
    
         throw $e;
     }
     return $order;
    

    }`

Actual result (*)

  1. The object_id is empty on event_type +order_placed.
  2. {"event_type":"order_placed","object_type":"order","object_id":"","object_increment_id":"000000022"}

Magento 2 order placed.txt

pmathbliss avatar Jul 17 '23 22:07 pmathbliss

Hi @pmathbliss. Thank you for your report. To speed up processing of this issue, make sure that you provided sufficient information. Add a comment to assign the issue: @magento I am working on this


Join Magento Community Engineering Slack and ask your questions in #github channel.

m2-assistant[bot] avatar Jul 17 '23 22:07 m2-assistant[bot]

Same issue here:

image

When try to run this command:

bin/magento inventory:reservation:list-inconsistencies -r | bin/magento inventory:reservation:create-compensations

this error happend:

There is an error in /var/www/html/vendor/magento/module-inventory-reservation-cli/Command/Input/GetReservationFromCompensationArgument.php at line: 105 Call to a member function getEntityId() on bool#0 /var/www/html/vendor/magento/module-inventory-reservation-cli/Command/CreateCompensations.php(135): Magento\InventoryReservationCli\Command\Input\GetReservationFromCompensationArgument->execute('585000609606:11...')

In de inventory_reservation table, I have missing object_id of some reservations:

image

This error causes some products in frotend to appear when they shouldn't at price 0:

image

ConradoSM avatar Aug 09 '23 18:08 ConradoSM

Hi @okolesnyk. Thank you for your request. I'm working on Magento instance for you.

Hi @okolesnyk, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.