[5.x]: Discounts: Can't select a variant for the "Only match certain purchasables…" field that is related to a Product Type that is not enabled on the "first" site.
What happened?
Description
Can't select a variant on "Only match certain purchasables…" field that is related to a Product Type that is not enabled on the "first" sites.
More context: The variant i am selecting belongs to a Product Type that is not enabled on all sites (3rd and 4th and not on the 1st and 2nd) of my setup. Since the Discounts has no "sites" concept (at least to relation to a site), the element returns null because it does not belong to the "first" site. On the other hand, if I choose a product that belongs to all sites, the discount is saved properly.
Steps to reproduce
- Create a discount that will match certain purchasable which belongs to a Product Type that is not enabled on the first site
- Save the discount
Expected behavior
Be saved properly with the variant(s) in the relation field
Actual behavior
Throw the error bellow:
TypeError: Cannot use "::class" on value of type null in /var/www/html/cms/vendor/craftcms/commerce/src/services/Discounts.php:868
Stack trace:
#0 /var/www/html/cms/vendor/craftcms/commerce/src/controllers/DiscountsController.php(350): craft\commerce\services\Discounts->saveDiscount(Object(craft\commerce\models\Discount))
#1 [internal function]: craft\commerce\controllers\DiscountsController->actionSave()
#2 /var/www/html/cms/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#3 /var/www/html/cms/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#4 /var/www/html/cms/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('save', Array)
#5 /var/www/html/cms/vendor/craftcms/cms/src/web/Application.php(361): yii\base\Module->runAction('commerce/discou...', Array)
#6 /var/www/html/cms/vendor/craftcms/cms/src/web/Application.php(660): craft\web\Application->runAction('commerce/discou...', Array)
#7 /var/www/html/cms/vendor/craftcms/cms/src/web/Application.php(323): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#8 /var/www/html/cms/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#9 /var/www/html/web/index.php(13): yii\base\Application->run()
#10 {main}
Craft CMS version
5.7.10
Craft Commerce version
5.7.0
PHP version
8.2
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
Hi @jesuismaxime
Thank you for your message.
We will take a look into this and see if we can replicate the behaviour you are seeing.
Thanks!
Hi @jesuismaxime
We believe we found the root of this issue.
We have editing discounts so that when selecting purchasable elements you should now see the site selection dropdown. This means you will be able to select elements from the correct site.
This fix will be included in the next release of Commerce.
To get this early, change your craftcms/commerce requirement in your project's composer.json to:
"require": {
"craftcms/commerce": "5.x-dev as 5.4.1.1",
"...": "..."
}
Then run composer update.
Thanks!
Commerce 5.4.2 has now been released with this fix included.
Thanks!
Sorry for the delay, hadnt work on that project since last June.
Just update the whole package(Craft, Commerce, plugins, etc.) and still have the same error.
Do I need to resave or delete/recreate something in other to make this fix work?
@jesuismaxime could you post a new GitHub issue with your versions etc and steps to reproduce. Thanks!