Integrating 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 for integrating the Google script into OpenCart CMS files. Than…
An article on how to integrate the Google Customer Reviews code into an OpenCart online store. This is a short example with instructions for integrating the Google script into OpenCart CMS files. Thanks to this, you will be able to receive reviews from customers who have already placed an order on your site. Buyers will get a reminder from Google that they can leave a review about your company.
Our guide will be useful for developers as well as for owners of OpenCart online stores who optimize their site themselves. Reviews in Google can significantly increase trust in your business, and this directly affects conversions. Getting reviews from customers is difficult, but if your products are worth praising, it will be simple, because Google itself will take care of asking your customer to rate the order or the work of your store. You just need to integrate the script on the order success page on the site.
How and where to add the Google Customer Reviews code in OpenCart
Google recommends adding the code to the order success page on your site, which is what we will do. Our guide is suitable for OpenCart 3 and OpenCart 4. If you are using OpenCart 2, you need to convert the twig code into PHP.
1. In the file catalog/controller/checkout/success.php
right after the line:
if (isset($this->session->data['order_id'])) { you 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/template/common/success.twig
before the line:
{{ footer }} you need to add:
{% if order_info %}
{% endif %}
It is worth noting that product GTINs are optional, so if you do not have them, you may omit them. In this example we pass the EAN code.
3. After this you need to refresh all possible caches you have. This can be done in the admin panel: modifications cache, twig template cache (theme cache), and the cache of any dedicated caching modules.
4. Place a test order — you should see the consent pop-up on the order success page. If you see it, everything is working correctly.
Overall, as you can see, integrating the Google Customer Reviews script into OpenCart is a fairly simple task. But it requires attention and focus to avoid mistakes. Happy selling!