commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[5.x]: Error variant missing its inventory item

Open jornwildenbeest opened this issue 1 year ago • 9 comments

What happened?

Description

Somehow, my best guest is during the upgrade to craft 5 but I am not sure, purchasables where created without inventory. This results in an error when you want to edit the variant in the cp.

And then there is (as far as I know) no way to re-save the variant or a way to add the inventory for the purchasable manually.

The error:

2025-01-07 08:41:01 [web.ERROR] [TypeError] TypeError: craft\commerce\services\Inventory::getInventoryItemById(): Argument #1 ($id) must be of type int, null given, called in /Users/user/Sites/site-x/vendor/craftcms/commerce/src/services/Inventory.php on line 81 and defined in /Users/user/Sites/site-x/vendor/craftcms/commerce/src/services/Inventory.php:88
Stack trace:
#0 /Users/user/Sites/site-x/vendor/craftcms/commerce/src/services/Inventory.php(81): craft\commerce\services\Inventory->getInventoryItemById(NULL)
#1 /Users/user/Sites/site-x/vendor/craftcms/commerce/src/base/Purchasable.php(903): craft\commerce\services\Inventory->getInventoryItemByPurchasable(Object(craft\commerce\elements\Variant))
#2 /Users/user/Sites/site-x/vendor/craftcms/commerce/src/fieldlayoutelements/PurchasableStockField.php(87): craft\commerce\base\Purchasable->getInventoryItem()
#3 /Users/user/Sites/site-x/vendor/craftcms/cms/src/fieldlayoutelements/BaseField.php(365): craft\commerce\fieldlayoutelements\PurchasableStockField->inputHtml(Object(craft\commerce\elements\Variant), false)
#4 /Users/user/Sites/site-x/vendor/craftcms/cms/src/models/FieldLayout.php(1013): craft\fieldlayoutelements\BaseField->formHtml(Object(craft\commerce\elements\Variant), false)
#5 /Users/user/Sites/site-x/vendor/craftcms/cms/src/web/View.php(1776): craft\models\FieldLayout->craft\models\{closure}()
#6 /Users/user/Sites/site-x/vendor/craftcms/cms/src/models/FieldLayout.php(1012): craft\web\View->namespaceInputs(Object(Closure), NULL)
#7 /Users/user/Sites/site-x/vendor/craftcms/cms/src/controllers/ElementsController.php(937): craft\models\FieldLayout->createForm(Object(craft\commerce\elements\Variant), false, Array)
#8 /Users/user/Sites/site-x/vendor/craftcms/cms/src/controllers/ElementsController.php(411): craft\controllers\ElementsController->_prepareEditor(Object(craft\commerce\elements\Variant), false, true, Object(craft\web\Response), 'cp-screen-38334...', Object(Closure), Object(Closure), Object(Closure))
#9 [internal function]: craft\controllers\ElementsController->craft\controllers\{closure}(Object(craft\web\Response), 'cp-screen-38334...')
#10 /Users/user/Sites/site-x/vendor/craftcms/cms/src/web/CpScreenResponseFormatter.php(67): call_user_func(Object(Closure), Object(craft\web\Response), 'cp-screen-38334...')
#11 /Users/user/Sites/site-x/vendor/craftcms/cms/src/web/CpScreenResponseFormatter.php(48): craft\web\CpScreenResponseFormatter->_formatJson(Object(craft\web\Request), Object(craft\web\Response), Object(craft\web\CpScreenResponseBehavior))
#12 /Users/user/Sites/site-x/vendor/yiisoft/yii2/web/Response.php(1109): craft\web\CpScreenResponseFormatter->format(Object(craft\web\Response))
#13 /Users/user/Sites/site-x/vendor/craftcms/cms/src/web/Response.php(339): yii\web\Response->prepare()
#14 /Users/user/Sites/site-x/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#15 /Users/user/Sites/site-x/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#16 /Users/user/Sites/site-x/web/index.php(12): yii\base\Application->run()
#17 /Users/user/.composer/vendor/laravel/valet/server.php(110): require('/Users/user/Sit...')
#18 {main} {"memory":29708304,"exception":"[object] (TypeError(code: 0): craft\\commerce\\services\\Inventory::getInventoryItemById(): Argument #1 ($id) must be of type int, null given, called in /Users/user/Sites/site-x/vendor/craftcms/commerce/src/services/Inventory.php on line 81 at /Users/user/Sites/site-x/vendor/craftcms/commerce/src/services/Inventory.php:88)"}

Steps to reproduce

Not sure how this happend, but is there a way to add a inventory manually or something?

Expected behavior

Create the inventory when it is missing.

Actual behavior

It throws an error, leaving no way to edit the variant.

Craft CMS version

5.5.6

Craft Commerce version

5.2.11

PHP version

8.3

Operating system and version

Mac OS 15.2 (24C101)

Database type and version

MySQL 8.*

Image driver and version

No response

Installed plugins and versions

  • Craft Commerce 5.2.11

jornwildenbeest avatar Jan 07 '25 08:01 jornwildenbeest

PT-2370 [5.x]:

linear[bot] avatar Jan 07 '25 08:01 linear[bot]

Hi @jornwildenbeest

Thank you for reporting.

As a matter of testing, in your local development environment can you run the CLI command craft resave/products and let us know if the problem persists?

Thanks!

nfourtythree avatar Jan 09 '25 09:01 nfourtythree

Hi @nfourtythree

Running the command craft resave/products did indeed fixed the issue.

Thanks

jornwildenbeest avatar Jan 09 '25 10:01 jornwildenbeest

Hi @jornwildenbeest

Glad to hear this fixed it.

Will close this for now but continue to monitor any reports related to this issue.

Thanks!

nfourtythree avatar Jan 09 '25 11:01 nfourtythree

Hi @nfourtythree,

We have a shop where this exact problem happened a few times now. In January and February we checked for missing inventory items and added them manually to the commerce_inventoryitems table and that fixed the problem for these variants.

We've thought it was maybe due to migrating data when launching the shop, but now 2 months later there are a few new variants with missing inventory items. So instead of just fixing them by resaving or adding a db record, I wonder if there's a bug in Commerce that leaves purchasables without an inventory item causing an error in the CP.

The shop is running on Craft 5.6.17 and Commerce 5.3.2.2

white-rick avatar Apr 30 '25 09:04 white-rick

I have seen the same thing (variants suddenly losing their inventory data with no apparent reason) and am wondering if anyone ever found any clues why this is happening?

elivz avatar May 20 '25 20:05 elivz

Hey folks, thanks for reporting this, sorry on the delay getting back to you.

We would love to see if we could get a copy of your database backup (as well as composer.json and composer.lock) for us to look at, as we haven't been able to reproduce this ourselves.

Please send to [email protected]

Also let us know:

  • Are you using any type of import tool like the Feedme plugin?
  • Do you have multiple inventory locations?

Thanks!

lukeholder avatar May 21 '25 10:05 lukeholder

Leaving this ticket open for a little longer although we are unable to reproduce. But encourage anyone experiencing this to please email [email protected] with a copy of your DB dump, composer.json and composer.lock - any steps to reproduce would also be great.

Would also be good to know if you have any custom product/variant creation or sync happening in a module or custom plugin.

Thanks.

lukeholder avatar Jul 30 '25 11:07 lukeholder

I am seeing this too. @lukeholder I just sent the files to Tommy regarding the duplicate key issue but perhaps you could use his install for this issue too? Go to the product id 5596797 and click the variant so the slidein happens. Then you get the error.

philipboomy avatar Oct 02 '25 14:10 philipboomy