craftcms / commerce

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

[5.x]: Unable to set default variant #3605

Closed white-lukas closed 3 weeks ago

white-lukas commented 1 month ago

What happened?

Description

when setting a default variant, after a save of the product the default variant is gone again, looking in the database on the defaultvariantId field in the product table, the ID seems to be set to a draft ID instead of the canonical ID

Craft CMS version

5.2.8

Craft Commerce version

5.0.13

PHP version

8.3.8

Operating system and version

Ubuntu 24.04

Database type and version

mysql 8.0

Image driver and version

No response

Installed plugins and versions

-

linear[bot] commented 1 month ago

PT-2008 [5.x]: Unable to set default variant

lukeholder commented 1 month ago

@white-lukas Could you please make a backup of your database, then try running these commands:

php craft gc

and then

php craft resave/products

and then let us know if this issue continues to occur.

If it does continue, please send us your database backup to support@craftcms.com - we will need to look into it.

Thanks

white-lukas commented 1 month ago

After the first resave, the default variants are set correctly again, but after saving the product after this again, the defaultVariant goes back to a draftId instead of the actual id

white-lukas commented 1 month ago

Hi @lukeholder,

Database is pretty large, so not that easy to send.

Did some more debugging, after saving the product I come in the craft\commerce\elements\Variant->afterSave() function with $isNew to true so it creates a new record, https://github.com/craftcms/commerce/blob/5.x/src/elements/Variant.php#L857 this one goes correct and the isDefault is getting set.

Could it be that https://github.com/craftcms/commerce/blob/5.x/src/elements/Variant.php#L870 should be canonicalId instead of id, If I change this in my code, it keeps the correct defaultVariant after resaves.

lukeholder commented 3 weeks ago

@white-lukas Can you please try 5.0.16 and let us know if this is resolved for you? You might need to save your product again.

white-lukas commented 3 weeks ago

Hi @lukeholder Updating seems to have solved the issue, Thanks