SwissUpLabs Logo

Miscellaneous scripts snippets

Here you can find some code snippets that can be used in Miscellaneous scripts of Checkout Success Page extension.

Google AdWords conversion tracking tag

It is just an example. send_to property has to have your own value.

<script>
    gtag('event', 'conversion', {'send_to': 'AW-123456789/AbC-D_efG-h12_34-567',
        'value': {{orderAmount}},
        'currency': '{{currency}}'
    });
</script>

Google Customer Reviews

Google Customer Reviews is a free program that allows Google to collect seller reviews on your behalf from your customers. Read more at Introducing Google Customer Reviews page.

Code below was taken from Add the opt-in code and slightly modified to work at checkout success page. Check source link to get more info about code below.

<!-- BEGIN GCR Opt-in Module Code -->
<script src="https://apis.google.com/js/platform.js?onload=renderOptIn"
  async defer>
</script>

<script>
  window.renderOptIn = function() {
    window.gapi.load('surveyoptin', function() {
      var shippingData = {{orderShippingAddress}},
          today = new Date(),
          estimatedDate = new Date();

      estimatedDate.setDate(today.getDate() + 3); // estimated delivery date is 3 days from now
      window.gapi.surveyoptin.render(
        {
          // REQUIRED
          "merchant_id":"MERCHANT_ID",
          "order_id": "{{orderId}}", // ORDER_ID
          "email": "{{customerEmail}}", // CUSTOMER_EMAIL
          "delivery_country": shippingData.country_id,
          "estimated_delivery_date": estimatedDate.toISOString().slice(0, 10),

          // OPTIONAL
          "opt_in_style": "OPT_IN_STYLE"
        });
     });
  }
</script>
<!-- END GCR Opt-in Module Code -->

Customer type (guest or registered customer)

<script type="text/javascript">
    var customerType = '{{customerId}}' ? 'registered' : 'guest';
</script>

Ordered Items SKUs

Requires Checkout Success Page version 1.1.3 or higher.

<script type="text/javascript">
    var orderedItems = {{orderItems}};
    var orderedSKUs = orderedItems.map(function(i){ return i.sku; }); // Array
</script>

Quantity of ordered products

Requires Checkout Success Page version 1.1.3 or higher.

<script type="text/javascript">
    var orderedItems = {{orderItems}};
    var orderedQty = orderedItems.reduce(function (q, i) { return q + parseFloat(i.qty_ordered); }, 0);
    // now orderedQty containes quantity of ordered products
</script>

Money total of ordered products without additional fees

Here is example how to calculate order total without any additional fees (shipping, taxes, discounts).

Requires Checkout Success Page version 1.1.3 or higher.

<script type="text/javascript">
    var orderedItems = {{orderItems}};
    var itemsTotal = orderedItems.reduce(function (t, i) { return t + parseFloat(i.row_total); }, 0);
    // now itemsTotal containes total of ordered items
</script>

Estimated delivery date

It’s just an example how you can calculate estimated delivary date.

In this case we know that shipping method with code FAST_DELIVERY deliver products within 1 day, THREE_DAYS_DELIVERY - deliver within 3 days and all other methods deliver within 4 days.

<script type="text/javascript">
    var today = new Date(),
        estimatedDate = new Date(),
        shippingMethod = '{{shippingCode}}';
    if (shippingMethod == 'FAST_DELIVERY') {
        estimatedDate.setDate(today.getDate() + 1);
    } else if (shippingMethod == 'THREE_DAYS_DELIVERY') {
        estimatedDate.setDate(today.getDate() + 3);
    } else {
        // all other methods delivers within 4 days
        estimatedDate.setDate(today.getDate() + 4);
    }
    // now estimatedDate containes delivery date depending
    // on shipping method customer used in order
</script>
Edit this Page