0

I'm using Drupal 10.2.4 with webform 6.2.2 I have a content type extras with a field_prix

langcode: fr
status: true
dependencies:
  config:
    - field.storage.node.field_prix
    - node.type.extras
id: node.extras.field_prix
field_name: field_prix
entity_type: node
bundle: extras
label: Prix
description: ''
required: true
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  min: null
  max: null
  prefix: ''
  suffix: €
field_type: decimal

i have a form with 2 fields : --extras for the end user to select the extras he wishes --prix_extras to add up the prices of the selected extras with a calculated twig

    extras:
  '#type': entity_checkboxes
  '#title': 'Extras :'
  '#title_display': before
  '#target_type': node
  '#selection_handler': 'default:node'
  '#selection_settings':
    target_bundles:
      extras: extras
    sort:
      field: title
      direction: ASC
prix_extras:
  '#type': computed_twig
  '#title': 'Montant des Extras : '
  '#title_display': before
  '#template': |
    {# Initialize total price variable #}
    {% set total_price = 0 %}

    {# Get the raw price values of selected extras using webform_token() #}
    {% set raw_prix_extras = webform_token('[webform_submission:values:extras:entity:field_prix]', webform_submission, [], options) %}

    raw_prix_extras {{ raw_prix_extras }}<br>

    {# Check if raw prices of extras are not empty #}
    {% if raw_prix_extras is not empty %}
        {# Remove the currency symbol '€' and trim raw prices #}
        {% set cleaned_prices = raw_prix_extras|replace({'€': ''})|trim %}
        cleaned_prices {{ cleaned_prices }}<br>

        {# Split cleaned prices into an array #}
        {% set prices_array = cleaned_prices|split(',') %}
        prices_array {{ prices_array }}<br>

        {# Loop through prices array to sum up the total price #}
        {% for price in prices_array %}
            {% set total_price = total_price + price|replace({' ': ''})|number_format(0, '.', '') %}
        {% endfor %}
    {% endif %}

    total_price {{ total_price }} €
  '#whitespace': spaceless
  '#store': true
  '#ajax': true

I have 2 extras : A = 20€ and B = 10€ If i select one or the other it returns the right price If i select both it retuns only the price of A

how to you get calculated twig to loop through the extras and add up the prices of each ?

1 Answer 1

1

It seems that the issue lies in the way the prices are added to the total_price variable. The prices are being concatenated as strings rather than being summed up as numbers. To correct this, we need to ensure that the prices are converted to numbers before adding them to total_price.

{# Initialize total price variable #}
{% set total_price = 0 %}

{# Get the raw price values of selected extras using webform_token() #}
{% set raw_prix_extras = webform_token('[webform_submission:values:extras:entity:field_prix]', webform_submission, [], options) %}

raw_prix_extras {{ raw_prix_extras }}<br>

{# Check if raw prices of extras are not empty #}
{% if raw_prix_extras is not empty %}
    {# Remove the currency symbol '€' and trim raw prices #}
    {% set cleaned_prices = raw_prix_extras|replace({'€': ''})|trim %}
    cleaned_prices {{ cleaned_prices }}<br>

    {# Split cleaned prices into an array #}
    {% set prices_array = cleaned_prices|split(',') %}
    prices_array {{ prices_array }}<br>

    {# Loop through prices array to sum up the total price #}
    {% for price in prices_array %}
        {% set total_price = total_price + price|float %}
    {% endfor %}
{% endif %}

total_price {{ total_price }} €
{# Initialize total price variable #}
{% set total_price = 0 %}

{# Get the raw price values of selected extras using webform_token() #}
{% set raw_prix_extras = webform_token('[webform_submission:values:extras:entity:field_prix]', webform_submission, [], options) %}

raw_prix_extras {{ raw_prix_extras }}<br>

{# Check if raw prices of extras are not empty #}
{% if raw_prix_extras is not empty %}
    {# Remove the currency symbol '€' and trim raw prices #}
    {% set cleaned_prices = raw_prix_extras|replace({'€': ''})|trim %}
    cleaned_prices {{ cleaned_prices }}<br>

    {# Split cleaned prices into an array #}
    {% set prices_array = cleaned_prices|split(',') %}
    prices_array {{ prices_array }}<br>

    {# Loop through prices array to sum up the total price #}
    {% for price in prices_array %}
        {% set total_price = total_price + price|float %}
    {% endfor %}
{% endif %}

total_price {{ total_price }} €

In this adjusted code, price|float ensures that each price is treated as a float (number) before being added to total_price. This should correctly sum up the prices as numbers.

let me know

1
  • it doesnot work. the array stays empty. it does not return multiple values. Only the 1st select item in the list....
    – Morgan P
    Commented Mar 24 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.