How to insert Google Merchant Review JS code in WooCommerce Order Complete page

荒凉一梦 提交于 2019-11-29 13:05:34
Raunak Gupta

If you want to add a JavaScript goal conversions code to your Order complete or Thankyou page then you have to use woocommerce_thankyou hook.

Here is the code:

function wh_CustomReadOrder($order_id) {
    //getting order object
    $order = wc_get_order($order_id);
    $email = $order->billing_email;
    ?>
    <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": mymerchantid,
                            "order_id": "<?php echo $order_id; ?>",
                            "email": "<?php echo $email; ?>",
                            "delivery_country": "COUNTRY_CODE",
                            "estimated_delivery_date": "YYYY-MM-DD"
                        }
                );
            });
        };
    </script>
    <?php
}

add_action('woocommerce_thankyou', 'wh_CustomReadOrder');

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin PHP files.
Code is tested and works.

Reference:

Related Questions

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!