[5.x]: Unable to set default variant
Closed this issue · 6 comments
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
@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
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
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.
@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.
Hi @lukeholder
Updating seems to have solved the issue, Thanks