questions

Issue with Dynamic Remarketing Tag. Conditional Logic causing a wordpress error

Rate this post

Trying to add google remarketing tag for products viewed to the WordPress footer, but I keep getting a critical error message. It works fine without the conditional logic, but once added I get an error. I’ve tried many combinations of things and am just spinning my wheels, getting no where. Can anyone help me get this working? I just can’t seem to figure out what’s wrong. Thanks in advance for helping.

”’

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': ‘<?php echo ( number_format( $price, 2, '.', '') ); ?>’,
    'items': [{
    'id': '<?php echo ( $product->id ); ?>',
    'google_business_vertical': 'retail'
    }]
  });
</script>

<?php
        }
    }
};

”’

Thank you

 

✔️Solution:

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
  }
};

Leave a Reply

Your email address will not be published.

Back to top button