Problem/Motivation

If there is no amount, tax amount or shipping amount then PriceTwigExtension is throwing error -

Uncaught PHP Exception InvalidArgumentException: "The "commerce_price_format" filter must be given a price object or an array with "number" and "currency_code" keys." at /var/www/d8Contrib/modules/contrib/commerce/modules/price/src/TwigExtension/PriceTwigExtension.php line 53, referer: https://d8contrib.dev/admin/commerce/reports/orders/1

Proposed resolution

If price is null, then we rather display 'NA' in the respective column.

Comments

sorabh.v6 created an issue. See original summary.

sorabh.v6’s picture

Assigned: sorabh.v6 » Unassigned
Status: Active » Needs review
StatusFileSize
new2.2 KB

If any of amount, taxAmount or shippingAmount is null. Listbuilder class sends 'NA' for them. Please review the patch.

harings_rob’s picture

I think the issue here is that we have price getters returning NULL.

I believe the correct behaviour should be that the getters always return a price object. But this price object can be 0 (not null).

I have provided a pull request with this proposal.

https://github.com/drupalcommerce/commerce/pull/842

smccabe’s picture

Status: Needs review » Reviewed & tested by the community

Agree with Rob, the price should always be zero, so that math can be done on it, instead of null or N/A. I believe this patch is good to go, there are failing tests but those happen even without this patch. So this is either RTBC or wait for those patches other testing issues to be fixed. Marking as RTBC in the interest of progress and not bottlenecking.

harings_rob’s picture

Project: Commerce Reporting » Commerce
Version: 8.x-1.x-dev » 7.x-2.0
Status: Reviewed & tested by the community » Needs work

The errors are in fact regressions. This issue should also be moved to the drupal commerce queue.

harings_rob’s picture

Project: Commerce » Commerce Core
Version: 7.x-2.0 » 8.x-2.x-dev
Component: Code » Price
casey’s picture

Status: Needs work » Needs review
StatusFileSize
new2.89 KB

The documentation of the commerce_price_format filter contains an example: {{ order.getTotalPrice|commerce_price_format }}

If this should be possible, the filter should allow NULL values.

nikathone’s picture

The patch is working for me. +1 for RTBC.

bojanz’s picture

Title: PriceTwigExtension throws error if amount is null » PriceTwigExtension::formatPrice() should accept empty values
Assigned: Unassigned » bojanz
Status: Needs review » Needs work

I agree that formatPrice() should accept an empty value.

However, there is no need for an $empty_price_fallback, Twig already gives us the "default" filter we can use, like this:

    {{ order.getTotalPrice|commerce_price_format|default('N/A') }}

I'll rework the patch.

  • bojanz committed 1838bc9 on 8.x-2.x
    Issue #2910190 by sorabh.v6, casey, harings_rob, bojanz:...
bojanz’s picture

Status: Needs work » Fixed

Done. Took the opportunity to clean up the tests as well. Thanks, everyone.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.