Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2.3]: getDiscountByCode(): Argument #1 ($code) must be of type string, null given #3045

Closed
kaspar-allenbach opened this issue Nov 24, 2022 · 3 comments
Assignees
Labels
bug commerce4 Issues related to Commerce v4 ℹ️ status: need more info When waiting for user to supply database or more information.

Comments

@kaspar-allenbach
Copy link
Contributor

What happened?

Description

I migrated to craft 4 according to the migration guide and everything went well.

Steps to reproduce

  1. Migrate to craft 4
  2. Add something into the cart
  3. On the Cart template there is the following error on the cart page:

craft\commerce\services\Discounts::getDiscountByCode(): Argument #1 ($code) must be of type string, null given, called in /Users/atelierworkstation/Documents/git-local/atelier-shop/vendor/twig/twig/src/Extension/CoreExtension.php on line 1607

Stack Trace


TypeError: craft\commerce\services\Discounts::getDiscountByCode(): Argument #1 ($code) must be of type string, null given, called in /vendor/twig/twig/src/Extension/CoreExtension.php on line 1607 and defined in /vendor/craftcms/commerce/src/services/Discounts.php:382
Stack trace:
#0 /vendor/twig/twig/src/Extension/CoreExtension.php(1607): craft\commerce\services\Discounts->getDiscountByCode(NULL)
#1 /vendor/craftcms/cms/src/helpers/Template.php(110): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\commerce\services\Discounts), 'getDiscountByCo...', Array, 'method', false, false)
#2 /storage/runtime/compiled_templates/48/4814239c41dedafc8391a1b70140a2d5.php(59): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(craft\commerce\services\Discounts), 'getDiscountByCo...', Array, 'method')
#3 /vendor/twig/twig/src/Template.php(171): __TwigTemplate_fc9f511b5bd2fbee71b2e2d81933d7e6->block_main(Array, Array)
#4 /storage/runtime/compiled_templates/1c/1c64b2042fe0dfe96716d9e6f05f4b31.php(332): Twig\Template->displayBlock('main', Array, Array)
#5 /vendor/twig/twig/src/Template.php(171): __TwigTemplate_f939a9e67f563b6befd17836bf1b98ec->block_body(Array, Array)
#6 /storage/runtime/compiled_templates/1c/1c64b2042fe0dfe96716d9e6f05f4b31.php(304): Twig\Template->displayBlock('body', Array, Array)
#7 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_f939a9e67f563b6befd17836bf1b98ec->doDisplay(Array, Array)
#8 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#9 /storage/runtime/compiled_templates/06/06430c5474c19a0e396cb203c104bc63.php(53): Twig\Template->display(Array, Array)
#10 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_f1d0c467d20bbb79bcf56d6786094c73->doDisplay(Array, Array)
#11 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#12 /storage/runtime/compiled_templates/48/4814239c41dedafc8391a1b70140a2d5.php(46): Twig\Template->display(Array, Array)
#13 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_fc9f511b5bd2fbee71b2e2d81933d7e6->doDisplay(Array, Array)
#14 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#15 /vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#16 /vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#17 /vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render(Array)
#18 /vendor/craftcms/cms/src/web/View.php(451): Twig\Environment->render('purchase/cart', Array)
#19 /vendor/craftcms/cms/src/web/View.php(504): craft\web\View->renderTemplate('purchase/cart', Array)
#20 /vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(56): craft\web\View->renderPageTemplate('purchase/cart', Array, 'site')
#21 /vendor/yiisoft/yii2/web/Response.php(1098): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#22 /vendor/craftcms/cms/src/web/Response.php(286): yii\web\Response->prepare()
#23 /vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#24 /vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#25 /www/index.php(32): yii\base\Application->run()
#26 {main}

Craft CMS version

4.4.3

Craft Commerce version

4.2.3

PHP version

8.1.1

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@kaspar-allenbach kaspar-allenbach added commerce4 Issues related to Commerce v4 bug labels Nov 24, 2022
@kaspar-allenbach kaspar-allenbach changed the title [4.x]: getDiscountByCode(): Argument #1 ($code) must be of type string, null given [4.2.3]: getDiscountByCode(): Argument #1 ($code) must be of type string, null given Nov 25, 2022
@kaspar-allenbach
Copy link
Contributor Author

Nothing?

@pdaleramirez pdaleramirez added the ℹ️ status: need more info When waiting for user to supply database or more information. label Dec 1, 2022
@pdaleramirez
Copy link
Contributor

@kaspar-allenbach It seems to be a templating issue. You are calling the getDiscountByCode somewhere in the site template without passing a value. Can you test this on the example templates to see if you are still having issues?

@pdaleramirez pdaleramirez self-assigned this Dec 5, 2022
@lukeholder
Copy link
Member

lukeholder commented Dec 6, 2022

@kaspar-allenbach Can you try craft.commerce.discounts.getDiscountByCode(cart.couponCode ?? '') in your example templates. The ?? '' will pass a string if no coupon is on the cart.

We will relax the getDiscountByCode's param type so that null can be passed again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce4 Issues related to Commerce v4 ℹ️ status: need more info When waiting for user to supply database or more information.
Projects
None yet
Development

No branches or pull requests

3 participants