cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.2 show the error in place order

SOLVED

Magento 2.2 show the error in place order

Order saving error: Notice: Undefined index: product_id in /home/ecommexsoln/public_html/staging/vendor/magento/module-catalog-inventory/Model/StockManagement.php on line 103

 

Please help me to solve this problem

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2.2 show the error in place order


Hi guys, today i faced the same issue. so i have fixed the issue and also found the cause for the issue, i will share with you, if you like thumps up.

 

Issue

Notice: Undefined index: product_id in /vendor/magento/module-catalog-inventory/Model/StockManagement.php on line 103

 

 

Steps to re-produce

   > add product into cart and do checkout process

   > after payment process done and before order save you will be receive this issue.

Log:   (Following log content only received)

 

2018-07-05T08:54:19+00:00 INFO (6): array (
6454 => 
array (
'type_id' => 'simple',
),
)

 

CauseSELECT * FROM cataloginventory_stock_item where product_id='6454'; 

and the result last column contains website_id, in my result website_id=1, this is wrong if i changed to website_id=0 then issue will be resolve, because by default the following file returning website_id is 0.

 

(vendor\magento\module-catalog-inventory\Model\Configuration.php):

public function getDefaultScopeId()
{
    // TODO: should be fixed in MAGETWO-46043
    // "0" is id of admin website, which is used in backed during save entity
    return 0;
}

 

Prevention

 -> Also update the following file to avoid issue like this.

(Note: overwrite file into your custom module then update)

File path: Magento\CatalogInventory\Model\StockManagement.php

-> this is the only condition added if(isset($lockedItemRecord['product_id']))

 

/**
     * Subtract product qtys from stock.
     * Return array of items that require full save.
     *
     * @param string[] $items
     * @param int $websiteId
     * @return StockItemInterface[]
     * @throws \Magento\Framework\Exception\LocalizedException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function registerProductsSale($items, $websiteId = null)
    {
        //if (!$websiteId) {
            $websiteId = $this->stockConfiguration->getDefaultScopeId();
        //}
        $this->getResource()->beginTransaction();
        $lockedItems = $this->getResource()->lockProductsStock(array_keys($items), $websiteId);
        $fullSaveItems = $registeredItems = [];
        foreach ($lockedItems as $lockedItemRecord) {
            if(isset($lockedItemRecord['product_id'])){ //Newly added
                $productId = $lockedItemRecord['product_id'];
                $this->stockRegistryStorage->removeStockItem($productId, $websiteId);
    
                /** @var StockItemInterface $stockItem */
                $orderedQty = $items[$productId];
                $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
                $stockItem->setQty($lockedItemRecord['qty']); // update data from locked item
                $canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
                if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
                    continue;
                }
                if (!$stockItem->hasAdminArea()
                    && !$this->stockState->checkQty($productId, $orderedQty, $stockItem->getWebsiteId())
                ) {
                    $this->getResource()->commit();
                    throw new \Magento\Framework\Exception\LocalizedException(
                        __('Not all of your products are available in the requested quantity.')
                    );
                }
                if ($this->canSubtractQty($stockItem)) {
                    $stockItem->setQty($stockItem->getQty() - $orderedQty);
                }
                $registeredItems[$productId] = $orderedQty;
                if (!$this->stockState->verifyStock($productId, $stockItem->getWebsiteId())
                    || $this->stockState->verifyNotification(
                        $productId,
                        $stockItem->getWebsiteId()
                    )
                ) {
                    $fullSaveItems[] = $stockItem;
                }
            }
        }
        $this->qtyCounter->correctItemsQty($registeredItems, $websiteId, '-');
        $this->getResource()->commit();
        
        return $fullSaveItems;
    }

 

Main causecataloginventory_stock_item,  pls check that where you did mistake whlie creating the  product.

 

Regards,

Maaraa

View solution in original post

5 REPLIES 5

Re: Magento 2.2 show the error in place order

Do you have any extensions which define plugins, overrides or events on the order save process?

If so, try disabling those and then try to reproduce the problem.





Magento Certification Directory:

https://u.magento.com/certification/directory/dev/498894/

Commerce Hero:

https://commercehero.io/simonfrost

Re: Magento 2.2 show the error in place order


Hi guys, today i faced the same issue. so i have fixed the issue and also found the cause for the issue, i will share with you, if you like thumps up.

 

Issue

Notice: Undefined index: product_id in /vendor/magento/module-catalog-inventory/Model/StockManagement.php on line 103

 

 

Steps to re-produce

   > add product into cart and do checkout process

   > after payment process done and before order save you will be receive this issue.

Log:   (Following log content only received)

 

2018-07-05T08:54:19+00:00 INFO (6): array (
6454 => 
array (
'type_id' => 'simple',
),
)

 

CauseSELECT * FROM cataloginventory_stock_item where product_id='6454'; 

and the result last column contains website_id, in my result website_id=1, this is wrong if i changed to website_id=0 then issue will be resolve, because by default the following file returning website_id is 0.

 

(vendor\magento\module-catalog-inventory\Model\Configuration.php):

public function getDefaultScopeId()
{
    // TODO: should be fixed in MAGETWO-46043
    // "0" is id of admin website, which is used in backed during save entity
    return 0;
}

 

Prevention

 -> Also update the following file to avoid issue like this.

(Note: overwrite file into your custom module then update)

File path: Magento\CatalogInventory\Model\StockManagement.php

-> this is the only condition added if(isset($lockedItemRecord['product_id']))

 

/**
     * Subtract product qtys from stock.
     * Return array of items that require full save.
     *
     * @param string[] $items
     * @param int $websiteId
     * @return StockItemInterface[]
     * @throws \Magento\Framework\Exception\LocalizedException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function registerProductsSale($items, $websiteId = null)
    {
        //if (!$websiteId) {
            $websiteId = $this->stockConfiguration->getDefaultScopeId();
        //}
        $this->getResource()->beginTransaction();
        $lockedItems = $this->getResource()->lockProductsStock(array_keys($items), $websiteId);
        $fullSaveItems = $registeredItems = [];
        foreach ($lockedItems as $lockedItemRecord) {
            if(isset($lockedItemRecord['product_id'])){ //Newly added
                $productId = $lockedItemRecord['product_id'];
                $this->stockRegistryStorage->removeStockItem($productId, $websiteId);
    
                /** @var StockItemInterface $stockItem */
                $orderedQty = $items[$productId];
                $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId);
                $stockItem->setQty($lockedItemRecord['qty']); // update data from locked item
                $canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem);
                if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) {
                    continue;
                }
                if (!$stockItem->hasAdminArea()
                    && !$this->stockState->checkQty($productId, $orderedQty, $stockItem->getWebsiteId())
                ) {
                    $this->getResource()->commit();
                    throw new \Magento\Framework\Exception\LocalizedException(
                        __('Not all of your products are available in the requested quantity.')
                    );
                }
                if ($this->canSubtractQty($stockItem)) {
                    $stockItem->setQty($stockItem->getQty() - $orderedQty);
                }
                $registeredItems[$productId] = $orderedQty;
                if (!$this->stockState->verifyStock($productId, $stockItem->getWebsiteId())
                    || $this->stockState->verifyNotification(
                        $productId,
                        $stockItem->getWebsiteId()
                    )
                ) {
                    $fullSaveItems[] = $stockItem;
                }
            }
        }
        $this->qtyCounter->correctItemsQty($registeredItems, $websiteId, '-');
        $this->getResource()->commit();
        
        return $fullSaveItems;
    }

 

Main causecataloginventory_stock_item,  pls check that where you did mistake whlie creating the  product.

 

Regards,

Maaraa

Re: Magento 2.2 show the error in place order

I tried but when i run setup:di:compile command I received new error:

Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in /home/evolutio/public_html/vendor/magento/module-catalog-inventory/Model/StockManagement.php on line 6

could you help me how can I resolve?

Re: Magento 2.2 show the error in place order

add to line106:

if(!isset($lockedItemRecord['product_id']))continue;

Re: Magento 2.2 show the error in place order

that saved my day buddy (my week perhaps?). This time was hard to track down.

We'd been debugging for hours (Magento 2.3.3), fixing another bug before getting here (https://github.com/magento/magento2/issues/24902).

without your patch was failing with any payment method.

thanks for sharing, highly appreciate it