commerce icon indicating copy to clipboard operation
commerce copied to clipboard

[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.

Open jesuismaxime opened this issue 8 months ago • 1 comments

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

  1. Create a discount that will match certain purchasable which belongs to a Product Type that is not enabled on the first site
  2. 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

jesuismaxime avatar Jun 23 '25 14:06 jesuismaxime

PT-2826 [5.x]:

linear[bot] avatar Jun 23 '25 14:06 linear[bot]

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!

nfourtythree avatar Jul 11 '25 08:07 nfourtythree

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!

nfourtythree avatar Jul 11 '25 09:07 nfourtythree

Commerce 5.4.2 has now been released with this fix included.

Thanks!

nfourtythree avatar Jul 16 '25 14:07 nfourtythree

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 avatar Aug 18 '25 19:08 jesuismaxime

@jesuismaxime could you post a new GitHub issue with your versions etc and steps to reproduce. Thanks!

lukeholder avatar Aug 19 '25 23:08 lukeholder