Integrate Google Customer Reviews Code into OpenCart

OpenCartBot - 04 April 2025
Integrate Google Customer Reviews Code into OpenCart

An article on how to integrate the Google Customer Reviews code into an OpenCart online store. This is a short example with instructions on how to integrate a Google script into OpenCart CMS files. This will allow you to receive reviews from customers who have already placed an order on your site. Buyers will receive reminders from Google that they can leave a review about your company.


How and where to add the Google Customer Reviews code to OpenCart

Google recommends adding the code to the successful checkout page on your site, which we will do. Our instructions are suitable for OpenCart 3 and OpenCart 4. If you have OpenCart 2, you need to convert the twig code to php.


1. In the catalog/controller/checkout/success.php file

immediately after the line:

if (isset($this->session->data['order_id'])) {

need to add:

$this->load->model('checkout/order');
$data['order_info'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);

$data['country_code'] = 'UA';
if (isset($data['order_info']['shipping_country_id'])){
	$this->load->model('localisation/country');
	$country = $this->model_localisation_country->getCountry($data['order_info']['shipping_country_id']);
	if ($country) {
		$data['country_code'] = $country['iso_code_2'];
	}
}

$data['delivery_date'] = date('Y-m-d', strtotime('+7 days'));

$data['gtins'] = [];
$products = $this->model_checkout_order->getOrderProducts($this->session->data['order_id']);
if ($products) {
	$eans_query = $this->db->query("SELECT ean FROM ".DB_PREFIX."product WHERE product_id IN ('".implode(', ', array_column($products, 'product_id'))."') AND ean != ''");
	$data['gtins'] = $eans_query->rows;
}


2. In the file catalog/view/theme/шаблон/template/common/success.twig

before the line:

{{ footer }}

need to add:

{% if order_info %}
<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": ID_ВАШОГО_МЕРЧАНТУ,
          "order_id": "{{ order_info.order_id }}",
          "email": "{{ order_info.email }}",
          "delivery_country": "{{ country_code }}",
          "estimated_delivery_date": "{{ delivery_date }}"
          {% if gtins %},
          "products": [{% for gtin in gtins %}{"gtin":"{{ gtin.ean }}"}{% if not loop.last %}, {% endif %}{% endfor %}]
          {% endif %}
        });
    });
  }
</script>
{% endif %}

It should be noted that product SKUs (gtin) are optional, so if you do not have SKUs, you can skip them. In this example we are passing the EAN code.


3. After that, you need to update all possible caches that you have, this can be done in the admin panel: modifier cache, twig template cache (theme cache), special caching module cache.


4. Make a test order, you should see a consent pop-up on the successful checkout page. If you see it, everything works properly.


In general, as you can see, integrating the Google Reviews script into OpenCart is a simple task. But it requires attention and concentration to avoid making a mistake. Happy selling!



Related Posts