1

I'm running a custom import.

At some point the error: The stock item was unable to be saved. Please try again.

Is triggered by : $this->_productRepository->save($prod);

Now I'm trying to figure out why.

MariaDB [magento]> select * from cataloginventory_stock;
+----------+------------+------------+
| stock_id | website_id | stock_name |
+----------+------------+------------+
|        1 |          0 | Default    |
+----------+------------+------------+

Is the fact I only have one stock there might be related? This script is launched by an URL from one of the front stores

MariaDB [magento]> select * from inventory_stock;
+----------+---------------+
| stock_id | name          |
+----------+---------------+
|        1 | Default Stock |
|        2 | Stock RE      |
|        3 | Stock YT      |
|        4 | Stock GF      |
|        5 | Stock MQ      |
|        6 | Stock BL      |
|        7 | Stock GP      |
|        8 | Stock SX      |
+----------+---------------+

I indeed have 8 websites (0 being an admin and 1 being base) website and stocks

EDIT : So I've been investigating a little bit more

It appears that my real error comes from vendor/magento/module-catalog/Model/CategoryRepository.php on the get method

He can't find the category

        /** @var Category $category */
        $category = $this->categoryFactory->create();
        if (null !== $storeId) {
            $category->setStoreId($storeId);
        }
        $category->load($categoryId);
        if (!$category->getId()) {
            var_dump($storeId);
            var_dump($categoryId);
            var_dump($category->getCollection()->getSelect()->__toString());
            throw NoSuchEntityException::singleField('id', $categoryId);
        }

Store id : 10 Category Id : 50

Error : No such entity with id = 50

Problem is, the category actually exist; she is even found when I log the collection matching the request

SELECT `main_table`.`entity_id`, `main_table`.`level`, `main_table`.`path`, `main_table`.`position`, `main_table`.`is_active`, `main_table`.`is_anchor` FROM `catalog_category_flat_store_10` AS `main_table`

Among other results I have this one :

+-----------+-------+---------------+----------+-----------+-----------+
| entity_id | level | path          | position | is_active | is_anchor |
+-----------+-------+---------------+----------+-----------+-----------+
|        50 |     2 | 1/2/50        |        2 |         1 |         1 |
1
  • It may be an elasticsearch issue.
    – yvzyldrm
    Commented Apr 14 at 14:59

1 Answer 1

1

In Magento 2+ version you can also update stocks using CatalogInventory Module...

$stockItem = $this->_stockRegistry->getStockItemBySku($productSku);
// $stockItem->setQtyCorrection($QtyOrdered); 
//if you use setQtyCorrection($var) instead of manually set Exact Qty, then it will increase Stock Qty with Ordered Qty. 
$stockItem->setQty(100);
$stockItem->setIsInStock((bool)100);
$this->_stockRegistry->updateStockItemBySku($productSku, $stockItem);

for above method use, you have to include

\Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,

For Full Example About Stock Update you can see My Update Answer Here & Also you can see this full example at my last question here

Even You can refer this Create Helper Class Function for Stock Update See this Answer, this helper function method also works, I tested it...

I used Magento 2.4 Version & its works for me...

I hope this one helps to you

2
  • If you found any issue for using above CatalogInventory module method , let me know Commented Aug 25, 2021 at 6:41
  • Hi, sorry but this doesn't seems to be stock related, he don't find the stock cause he don't find the category.
    – Claims
    Commented Aug 25, 2021 at 8:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.