1

I would like to use Google Customer Reviews on my webshop, and I have received a snipplet that I am to insert on my succes page.

The snipplet is here:

<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": XXXXXXXXX,
          "order_id": "ORDER_ID",
          "email": "CUSTOMER_EMAIL",
          "delivery_country": "COUNTRY_CODE",
          "estimated_delivery_date": "YYYY-MM-DD"
        });
    });
  }
</script>

But I dont know which values to put in these fields:

      "order_id": "ORDER_ID",
      "email": "CUSTOMER_EMAIL",
      "delivery_country": "COUNTRY_CODE",
      "estimated_delivery_date": "YYYY-MM-DD"

Can you help me?

  • Could you explain a bit more then only "not working good"? – Myron May 3 '17 at 14:34
5

Most likely you need to only display this on the Checkout Success page (read the documentation to verify).

You can find the success HTML here:

app/design/frontend/PACKAGE/THEME/template/checkout/success.phtml

(If your theme doesn't have that file, then look at the base/default theme.)

In the PHTML, $this = Mage_Checkout_Block_Success which inefficiently loads the getRealOrderId() with the following function:

public function getRealOrderId()
{
    $order = Mage::getModel('sales/order')->load($this->getLastOrderId());
    #print_r($order->getData());
    return $order->getIncrementId();
}

To get the additional data, you need to load the entire order.

Most implementations I have seen have actually loaded the order directly in the success.phtml... and while normally that is frowned upon, its more efficient than trying to rewrite the success block and adding the functionality that SHOULD be there to begin with (I have no idea why the entire order was loaded, but not available for the success page).

Once you have the order loaded, you can get:

<script>
  window.renderOptIn = function() {
    window.gapi.load('surveyoptin', function() {
      window.gapi.surveyoptin.render(
        {
          "merchant_id": XXXXXXXXX,
          "order_id": "<?php echo $order->getIncrementId() ?>",
          "email": "<?php echo $order->getCustomerEmail() ?>",
          "delivery_country": "<?php echo $order->getShippingAddress()->getCountryId() ?>",
          "estimated_delivery_date": "YYYY-MM-DD"
        });
    });
}
</script>

Determining the estimated delivery date is something that is not part of the core functionality and you will probably just need to guess. In reality, this may not matter that much for the review.

Also the country code above is the 2-letter ISO code. I don't remember if Google expects this one or the 3-letter code... you might need to convert it. If the order is virtual (no shipping info), then this line will fail with a null value, you can add logic to switch to the billing address if the order is virtual.

| improve this answer | |
  • Thank you! I have inserted you code to the success.phtml. But when completing an order I find this error in the "source code". " Fatal error: Call to a member function getIncrementId() on null in xxxxxxx/template/checkout/success.phtml on line 57 " Line 57 is the order_id I have no idea what is wrong - sorry :-) Can you help? – Thomas Kramer Dec 13 '16 at 7:17
  • You need to add this to the top of the success.phtml: $order = Mage::getModel('sales/order')->load($this->getLastOrderId()); – Phil M Dec 13 '16 at 16:27
  • Just all on its own before my first div? I dont know anything about php, sorry – Thomas Kramer Dec 14 '16 at 14:53
  • 1
    Before the first call to order, yes (near the top is fine), but you have to wrap it in <?php $order = Mage::getModel('sales/order')->load($this->getLastOrderId())‌​; ?> (make sure its not inside another <?php ?> block) – Phil M Dec 15 '16 at 16:34
  • 1
    I get this: Parse error: syntax error, unexpected '‌​‌​' (T_STRING) in /xxxxxxxx/template/checkout/success.phtml on line 27 – Thomas Kramer Dec 19 '16 at 15:16
1

For estimated delivery date you can do the following: In the code you can change the format of the date and the estimated delivery I put 6 days. I hope it helps

"estimated_delivery_date": "<?php $date = date("d-m-Y"); $mod_date = strtotime($date."+ 6 days"); echo date("d-m-Y",$mod_date); ?>"
| improve this answer | |
1

enter image description here I test this code on Magento 1.9.It is working.

| improve this answer | |
  • Please post code and not an image – TheWebsiteGuy Sep 11 '17 at 15:35
  • I know he's supposed to be posting actual code but this one is the only answer worked on my Magento site. Tried other answers but got error. Thanks – Mas Apr 9 '18 at 3:43
1

Here is fully tested working script

<?php 
    $orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
    $order = Mage::getModel('sales/order')->load($orderId);
?>
<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": YOUR MERCHANT ID,
          "order_id": "<?php echo $order->getIncrementId() ?>",
          "email": "<?php echo $order->getCustomerEmail() ?>",
          "delivery_country": "<?php echo $order->getShippingAddress()->getCountryId() ?>",
          "estimated_delivery_date": "YYYY-MM-DD"
        });
    });
  }
</script>

This need to be added to your success.phtml

For estimated date you can follow this Showing an estimated delivery date

| improve this answer | |
0
<?php
$orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
?>

<script>
window.renderOptIn = function() {
window.gapi.load('surveyoptin', function() {
window.gapi.surveyoptin.render(
{
"merchant_id": XXXXXXXXX,
    "order_id": "<?php echo $orderId; ?>",
    "email": "<?php $order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId()); echo $order->getCustomerEmail(); ?>",
    "delivery_country": "US",
    "estimated_delivery_date": "<?php $date = date("d-m-Y"); $mod_date = strtotime($date."+ 6 days"); echo date("d-m-Y",$mod_date); ?>"

});
});
}
</script>

This is how I was able to get the script to load in the customer information - have not confirmed that it sends the info to google however - I do see the customer information in the inspector.

| improve this answer | |
0

This is the full working code I use, including language snippet and current date +2 days

<!-- BEGIN GCR Opt-in Module Code -->
<?php
   $startdate = strtotime("now");
   $enddate = strtotime("+48 hours", $startdate);
   $currdate = date("Y-m-d", $enddate);
?> 
<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": *****,
          "order_id": "<?php echo $order->getIncrementId() ?>",
          "email": "<?php echo $order->getCustomerEmail() ?>",
          "delivery_country": "<?php echo $order->getShippingAddress()->getCountryId() ?>",
          "estimated_delivery_date": "<? echo $currdate ?>",

          // OPTIONAL FIELDS
          "products": [{"gtin":"GTIN1"}, {"gtin":"GTIN2"}]
        });
    });
  }
</script>
<!-- END GCR Opt-in Module Code -->
<!-- BEGIN GCR Language Code -->
<script>
  window.___gcfg = {
    lang: 'en_GB'
  };
</script>
<!-- END GCR Language Code -->
| improve this answer | |
  • 1
    Thank you very much. Is there a way to test if it works, without having to complete an order? – Thomas Kramer Mar 6 '18 at 14:38
  • Unfortunately I've not looked into this. Its on a production site and I've not got to the test phase yet... I'll let you know if I find anything – 07lodgeT Mar 7 '18 at 8:08
  • I've looked into this a little more now. To display a seller rating in your badge you need at least 150 ratings. [ support.google.com/merchants/answer/7124326 ] (Google Customer Reviews FAQs) Your Google Customer Reviews badge will state 'Rating not available' instead of a rating (i.e. the numerical value and star rating). A minimum of 150 reviews is required to establish a seller rating. – 07lodgeT Mar 8 '18 at 12:12
  • @ThomasKramer the only way I could test was to use a test/sandbox payment gateway on my local development site, and temporarily change my hosts file so that the production domain points to my local IP, since you can only have one URL set up and it needs to be verified. – regularmike May 15 '19 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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