Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory Reserveration order_placed #3392

Open
pmathbliss opened this issue Jul 17, 2023 · 4 comments
Open

Inventory Reserveration order_placed #3392

pmathbliss opened this issue Jul 17, 2023 · 4 comments

Comments

@pmathbliss
Copy link

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

@m2-assistant
Copy link

m2-assistant bot commented Jul 17, 2023

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.

@ConradoSM
Copy link

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

@magento-deployment-service
Copy link

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

@magento-deployment-service
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Backlog
  
Ready for Grooming
Development

No branches or pull requests

2 participants