Search code examples
javascripttwigopencart-3

Twig variables to javascript array.push


I want to pass twig variables to javascript .push

My code in twig file:

var order_id = '{{ order_id }}',
  total = '{{ total }}';
console.log(order_id);
console.log(total);
(window.b24order = window.b24order || []).push({ id: order_id, sum: total });
console.log(window.b24order)

I see the values in console but in array.push is nothing


Solution

  • You overwrite your window.b24order array with the return value of push(), which returns the new length, not the modified array. The array is already modified.

    JavaScript Array.push

    Change to this approach:

        var order_id = '{{ order_id }}',
          total = '{{ total }}';
        console.log(order_id);
        console.log(total);
        window.b24order = window.b24order || [];
        window.b24order.push({ id: order_id, sum: total });
        console.log(window.b24order)