1

I am trying to implement Google Customer Reviews into my checkout page on my Wordpress website.

I am running Woocommerce and I've implemented the below code-snippet into my checkout page.

<script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script>

<script>
  window.renderOptIn = function() {
    window.gapi.load('surveyoptin', function() {
      window.gapi.surveyoptin.render(
        {
          // REQUIRED FIELDS
          "merchant_id": XXXXXX,
          "order_id": "ORDER_ID",
          "email": "CUSTOMER_EMAIL",
          "delivery_country": "COUNTRY_CODE",
          "estimated_delivery_date": "YYYY-MM-DD",

          // OPTIONAL FIELDS
          "products": [{"gtin":"GTIN1"}, {"gtin":"GTIN2"}]
        });
    });
  }
</script>

This script seems to work correctly when I hard-code my email address, order ID and country code.

However, I wish to extract the order ID, customer_email and others from the checkout page on which the script is active. I am unable to find a way how to do this. I did find this link which talks about an order object: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

Even with this information, I don't know how to extract the order details from the webpage and feed them as parameters inside the code snippet.

I've also tried Google's example (found here: https://support.google.com/merchants/answer/7106244):

<!-- 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() {

window.gapi.surveyoptin.render(

{

"merchant_id": 42,

"order_id": "<?php echo $order_id ?>",

"email": "<?php echo $email_address ?>",

"delivery_country": "<?php echo $user_country ?>",

"estimated_delivery_date": "<?php echo $delivery_date ?>",

"products": [{"gtin":"<?php echo $gtin_1 ?>"}, {"gtin":"<?php echo $gtin_2 ?>"}],

"opt_in_style": "BOTTOM_LEFT_DIALOG"

});

});

}

</script>

<!-- END GCR Opt-in Module Code -->

<!-- BEGIN GCR Language Code -->

<script>

window.___gcfg = {

lang: 'en_US'

};

</script>

<!-- END GCR Language Code -->

This example tries to echo the values into the required fields.

My two questions:

  • How do I find out which objects are present on my checkout page?
  • How do I call the objects to extract data from it to feed to the code-snippet?
SirMario
  • 31
  • 3
  • The trick is to generate the JS snippet on the server side. Here is the list of [hooks](https://woocommerce.github.io/code-reference/hooks/hooks.html), pick one that works, gather the required data in the hook and construct the JS code. PHP will send it to the browser. For reference see this [thread](https://stackoverflow.com/questions/42313018/woocommerce-get-orders-on-thank-you-page-and-pass-data-javascript-snippet) – Anand Shah Jul 25 '22 at 04:24
  • @AnandShah When I add the plugin from the thread you mentioned, my site breaks down. I have to remove the plugin file from the file editor to make it work again. – SirMario Jul 25 '22 at 12:53
  • I posted the thread link for reference so that you can adapt the code to your needs. What is the error message you are getting? – Anand Shah Jul 26 '22 at 14:34

0 Answers0