doushishi6513 2017-03-18 16:21
浏览 58
已采纳

如何在WooCommerce Order Complete页面中插入Google Merchant Review JS代码

I want to complete the variables the Google Merchant Review codes asks to be completed on the checkout page:

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

I need to echo the following variables:

ORDER_ID: ID number of the WooCommerce order ID

CUSTOMER_EMAIL: The email listed in the customer information section of the order

DELIVERY_COUNTRY: I think I can just fill it with ES since I only sell in Spain

ESTIMATED_DELIVERY_DATE: I already have a function I use to calculate the shipping date, so I guess I can use that php function here.

In conclusion, I would need help figuring out how do I echo the ORDER_ID and CUSTOMER_EMAIL in the checkout page, concretely inside of said script. I'm kind of clueless on how to do so, since all I tried had kind of a catastrophic result

Thank you very much for reading!

TL;DR: How do I get to echo the ORDER_ID and CUSTOMER_EMAIL in the after payment checkout page on WooCommerce?

  • 写回答

1条回答 默认 最新

  • doudeng8691 2017-03-18 18:39
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?