woocommerce offtopic - I want to run a script only once on a 'thank you' page

admin2025-04-20  1

I'm wondering what the standard way is to avoid executing some code when a user refreshes the page. We have a unique ID (order number) so we can keep it server side, there's no need to set a cookie.

On our WooCommerce 'Thank You' page we call the Facebook Pixel 'Purchase' event. However some mobile device users are opening their browser the next day and the page is requested again which adds a duplicate pixel event, skewing our data.

I experimented with transients, but the below code resulted in no events firing. I installed the Transient Manager plugin and cannot see anything set, but I understand WPEngine does have some customisations which may be changing how things work though I'm not across the full details.

<?php 
$transient_id = 'fbpixel-' . $order->get_order_number();
if ( $order->get_subtotal() > 0 && ( false === ( $pixel_fired = get_transient( $transient_id ) ) ) ) : 
    set_transient( $transient_id, $order->get_order_number(), 3 * DAY_IN_SECONDS );
?>
<script>
    fbq('track', 'Purchase', {currency: "AUD", value: <?php echo ($order->get_total() - $order->get_total_shipping()); ?> });
</script>
<?php endif; ?>

My understanding also is that transients may be deleted at any time for no reason even before the expiration time, so they may not be an appropriate solution.

I'm wondering what the standard way is to avoid executing some code when a user refreshes the page. We have a unique ID (order number) so we can keep it server side, there's no need to set a cookie.

On our WooCommerce 'Thank You' page we call the Facebook Pixel 'Purchase' event. However some mobile device users are opening their browser the next day and the page is requested again which adds a duplicate pixel event, skewing our data.

I experimented with transients, but the below code resulted in no events firing. I installed the Transient Manager plugin and cannot see anything set, but I understand WPEngine does have some customisations which may be changing how things work though I'm not across the full details.

<?php 
$transient_id = 'fbpixel-' . $order->get_order_number();
if ( $order->get_subtotal() > 0 && ( false === ( $pixel_fired = get_transient( $transient_id ) ) ) ) : 
    set_transient( $transient_id, $order->get_order_number(), 3 * DAY_IN_SECONDS );
?>
<script>
    fbq('track', 'Purchase', {currency: "AUD", value: <?php echo ($order->get_total() - $order->get_total_shipping()); ?> });
</script>
<?php endif; ?>

My understanding also is that transients may be deleted at any time for no reason even before the expiration time, so they may not be an appropriate solution.

Share Improve this question asked Oct 1, 2019 at 2:22 Nick PNick P 1034 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

You can use order post meta to check order already there or not. try below code. let me know if this helps you.

$order_id = $order->get_id();
$fbpixel = get_post_meta($order_id, 'fbpixel-'.$order->get_order_number(),true);
if ( $order->get_subtotal() > 0 && ( empty( $fbpixel ) ) ) : 
  update_post_meta($order_id, 'fbpixel-'.$order->get_order_number(),1);
  ?>
  <script>
      fbq('track', 'Purchase', {currency: "AUD", value: <?php echo ($order->get_total() - $order->get_total_shipping()); ?> });
  </script>
<?php endif;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745116435a285887.html

最新回复(0)