首页 > 解决方案 > 动态再营销代码问题。导致wordpress错误的条件逻辑

问题描述

尝试为查看到 WordPress 页脚的产品添加谷歌再营销标签,但我不断收到一条严重的错误消息。没有条件逻辑它可以正常工作,但是一旦添加我就会出错。我已经尝试了很多东西的组合,只是在旋转我的轮子,却无处可去。谁能帮我搞定这个工作?我似乎无法弄清楚出了什么问题。提前感谢您的帮助。

'''

add_action('wp_footer’, 'dehydr8ed_footer_tags');
function dehydr8ed_footer_tags() {

    if (is_product() ){
        global $post;
        $post_id = $post->ID;
        $product = wc_get_product( $post->ID );
        if ( $product->is_in_stock() ) {
        $price = $product->get_price();

?>

<script>
  gtag('event', 'page_view', {
    'send_to': '#########',
    'value': ‘&lt;?php echo ( number_format( $price, 2, '.', '') ); ?>’,
    'items': [{
    'id': '<?php echo ( $product->id ); ?>',
    'google_business_vertical': 'retail'
    }]
  });
</script>

<?php
        }
    }
};

'''

谢谢你,JD

标签: wordpresswoocommercegtag.js

解决方案


解决了...

add_action('wp_footer', 'dehydr8ed_footer_tags');
function dehydr8ed_footer_tags(){

    if(is_product()) {
        global $product;
        $id = $product->get_id();
        if ( $product->is_in_stock() ) {
        $price = $product->get_price();
}?>
<script>
  gtag('event', 'page_view', {
    'send_to': '##########',
    'value': $<?php echo ( number_format( $price, 2, '.', '') ); ?>’,
    'items': [{
    'id': '<?php echo esc_js( $product->id ); ?>',
    'google_business_vertical': 'retail'
    }]
  });
</script>
<?php
  }
};

推荐阅读