craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
227 stars 170 forks source link

[5.x]: TypeError when trying to change inventory level of purchasable that is not populate to the primary site #3788

Open white-lukas opened 3 days ago

white-lukas commented 3 days ago

What happened?

Description

When having a commerce setup with 2 sites and 2 stores and the product propagation is set to save product to other sites in the same site group (the 2 sites are in different groups) and you try to set the inventory level of a product that is propagated to the non-primary site, you get a TypeError TypeError: craft\commerce\models\InventoryLevel::getPurchasable(): Return value must be of type craft\commerce\base\Purchasable, null returned in /var/www/html/vendor/craftcms/commerce/src/models/InventoryLevel.php:135

Digging deeper into the query that is getting executed when trying to get the purchasable, there is no specific site passed to the query (https://github.com/craftcms/commerce/blob/5.x/src/models/InventoryItem.php#L66), but the query that is getting executed does have a siteId = {primarySiteId} parameter, which lead to no purchasable is being found.

Steps to reproduce

  1. commerce setup with 2 sites/stores with propagation method per site
  2. try to change inventory level of non primary site
  3. TypeError and unable to set stock levels

Expected behavior

The correct purchasable for the site is returned and you are able to change the inventory levels

Craft CMS version

5.5.3

Craft Commerce version

5.2.6

PHP version

8.3.12

Operating system and version

Ubuntu 24.04

Database type and version

mysql 8.0.37

Image driver and version

No response

Installed plugins and versions

No response

linear[bot] commented 3 days ago

PT-2326 [5.x]: TypeError when trying to change inventory level of purchasable that is not populate to the primary site