Magento – Magento and Google Customer Reviews

magento-1.9

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?

Best Answer

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.

Related Topic