[5.x]: craft\commerce\elements\Order::setPaymentCurrency(): Argument #1 ($value) must be of type string, null given
What happened?
Hello. This is a bug for example template.
Description
Making a payment for the outstanding balance of a partially paid order using the example template throws this error.
Steps to reproduce
- Enable partial payment for a store.
- Make a partial payment on an order.
- Visit my order page and select the relevant order.
- Click the 'Make Payment' button to pay the outstanding balance.
- Select the Dummy gateway.
TypeError: craft\commerce\elements\Order::setPaymentCurrency(): Argument #1 ($value) must be of type string, null given, called in /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php on line 1861 and defined in /var/www/html/vendor/craftcms/commerce/src/elements/Order.php:3404
Stack trace:
#0 /var/www/html/vendor/twig/twig/src/Extension/CoreExtension.php(1861): craft\commerce\elements\Order->setPaymentCurrency()
#1 /var/www/html/vendor/craftcms/cms/src/helpers/Template.php(148): Twig\Extension\CoreExtension::getAttribute()
#2 /var/www/html/storage/runtime/compiled_templates/e7/e71bd81f211bf15f30bfb5e9fed00dd8.php(266): craft\helpers\Template::attribute()
#3 /var/www/html/vendor/twig/twig/src/Template.php(431): __TwigTemplate_583fae0a3ea388bfd988f31bf8c82e17->block_main()
#4 /var/www/html/storage/runtime/compiled_templates/39/391f2e59b209d8ac25f5d9417489ca5b.php(399): Twig\Template->yieldBlock()
#5 /var/www/html/vendor/twig/twig/src/Template.php(431): __TwigTemplate_3ca12679132cb4bd8493f1a541f6d179->block_body()
#6 /var/www/html/storage/runtime/compiled_templates/39/391f2e59b209d8ac25f5d9417489ca5b.php(303): Twig\Template->yieldBlock()
#7 /var/www/html/vendor/twig/twig/src/Template.php(387): __TwigTemplate_3ca12679132cb4bd8493f1a541f6d179->doDisplay()
#8 /var/www/html/storage/runtime/compiled_templates/e7/e71bd81f211bf15f30bfb5e9fed00dd8.php(81): Twig\Template->yield()
#9 /var/www/html/vendor/twig/twig/src/Template.php(387): __TwigTemplate_583fae0a3ea388bfd988f31bf8c82e17->doDisplay()
#10 /var/www/html/vendor/twig/twig/src/Template.php(343): Twig\Template->yield()
#11 /var/www/html/vendor/twig/twig/src/Template.php(358): Twig\Template->display()
#12 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(35): Twig\Template->render()
#13 /var/www/html/vendor/twig/twig/src/Environment.php(320): Twig\TemplateWrapper->render()
#14 /var/www/html/vendor/craftcms/cms/src/web/View.php(581): Twig\Environment->render()
#15 /var/www/html/vendor/craftcms/cms/src/web/View.php(634): craft\web\View->renderTemplate()
#16 /var/www/html/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(57): craft\web\View->renderPageTemplate()
#17 /var/www/html/vendor/yiisoft/yii2/web/Response.php(1109): craft\web\TemplateResponseFormatter->format()
#18 /var/www/html/vendor/craftcms/cms/src/web/Response.php(341): yii\web\Response->prepare()
#19 /var/www/html/vendor/yiisoft/yii2/web/Response.php(340): craft\web\Response->prepare()
#20 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#21 /var/www/html/web/index.php(12): yii\base\Application->run()
#22 {main}
Craft CMS version
5.7.4
Craft Commerce version
5.3.10
PHP version
No response
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
Taking a look at this today and will let you know what I find. Thanks.
@vnali Can you resave your payment currencies in the control panel and try again?
@lukeholder i have only one currency as USD (Primary) in admin/commerce/store-management/primary/payment-currencies which there is no way i can resave it.
More details: I tried to inspect which parameters are passed when paying the remaining balance via /shop/customer/order and after that selecting a gateway, and I noticed that paymentCurrency is not provided in the pay-static.twig template.
Here’s what the parameters look like:
array:3 [
"number" => "d3f8..."
"email" => "xyz.."
"gatewayId" => "1"
]
However, on line 131 of pay-static.twig, there's this line:
{% do cart.setPaymentCurrency(paymentCurrency) %}
The problem is that paymentCurrency is required, but it’s not being passed.
@vnali can you pass paymentCurrency in the request to pay, and not use the do cart.setPaymentCurrency setting it in the template will not save it on the order, so when the pay request is made, the paymentCurrency will not be set.
sorry @lukeholder I think I explained myself poorly, so I uploaded a video to explain it better. I didn’t write the template myself; I used the example templates from commerce repo here
https://github.com/user-attachments/assets/51162ddb-cf0b-49a9-b20e-fac9ed7889ea
Thanks for reporting. I fixed the example templates in this PR: https://github.com/craftcms/commerce/pull/4083
You are welcome to look at the changes and update your example templates directly, alternatively we will let you know here once the PR is included in a release.
Thanks.
Commerce 5.4.4 is out with that change.