Order number Not Available On Order Status Page

I have installed GTM tag in Checkout > Order Status Page > Additional Script section. The script is to track purchase event with additional information such as order number, amount, and currency.

window.dataLayer = window.dataLayer || [];

{% assign order_number_var =  "" %}

{% if order.order_number %} {% assign order_number_var = order.order_number %} 
{% elsif order_number %} {% assign order_number_var = order_number %} 
{% elsif checkout.order_number %} {% assign order_number_var = checkout.order_number %} 
{% endif %}

{% assign orderValue =  0 %}
{% if order.total_price %}{% assign orderValue = order.total_price | money_without_currency %}
{% elsif checkout.total_price %}{% assign orderValue = checkout.total_price | money_without_currency %}
{% endif %}

dataLayer.push({
"event": "purchase",
"ecommerce": {
  "orderId": "{{order_number_var}}",
  "value": "{{orderValue}}",
  "currency": "{{checkout.currency}}"
  }
});

In GTM, I set up a script to be fired upon the event “purchase” is pushed to the dataLayer.

It does send the data, but I am facing intermittent missing information. Order number, currency, and total amount are sometimes not available for some orders. But the event “purchase” is pushed, so script in GTM is triggered.
Is there anything missing my setup or what would be the reason for missing values? Any insight would be greatly appreciated. Thanks in advance.