• I am trying to pass the WooCommerce order total back to Facebook via thier tracking pixel.

    Using this code in my functions.php file I can pass back the entire order total (product cost and shipping cost). But I want it to only pass back the product cost (order total without shipping).

    // FB Pixel Tracking Code
        add_action( 'woocommerce_thankyou', 'fb_pixeltracking' );
    
        function fb_pixeltracking( $order_id ) {
           $order = new WC_Order( $order_id );
           $order_total = $order->get_total();
         ?>
        <!-- Start FB Tracking - Replace XXXXXXXXXXXXXX with your tracking ID to track orders with values-->
    
        <!-- Facebook Conversion Code for Sales from Facebook Ads -->
        <script>(function() {
         var _fbq = window._fbq || (window._fbq = []);
         if (!_fbq.loaded) {
           var fbds = document.createElement('script');
           fbds.async = true;
           fbds.src = '//connect.facebook.net/en_US/fbds.js';
           var s = document.getElementsByTagName('script')[0];
           s.parentNode.insertBefore(fbds, s);
           _fbq.loaded = true;
         }
        })();
        window._fbq = window._fbq || [];
        window._fbq.push(['track', 'XXXXXXXXXXXXXX', {'value':'<?php echo $order_total ?>','currency':'GBP'}]);
        </script>
        <noscript>
        <img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/trev=XXXXXXXXXXXXXX&cd[value]=<?php echo $order_total ?>&cd[currency]=GBP&noscript=1" />
        </noscript>
         <!-- END FB Tracking -->
    
        <?php
        }

    Full details for implementing this piece of code can be found here: https://www.social-response.co.uk/facebook-conversion-pixel-woocommerce-values/

    It is using <?php echo $order_total ?> to pass back the order total including shipping. What should I be using to pass back the order total without shipping?

    https://www.ads-software.com/support/plugin/woocommerce

Viewing 8 replies - 1 through 8 (of 8 total)
  • You can get the items total by calling the WC_Order::get_subtotal() method. Quite simply:

    function fb_pixeltracking( $order_id ) {
      $order = new WC_Order( $order_id );
      $order_total = $order->get_total();
      $order_total_without_shipping = $order->get_subtotal();
    }
    Thread Starter bruceleebee

    (@bennygill)

    Thanks for the help!!

    For this line of code in my original post:

    window._fbq.push(['track', 'XXXXXXXXXXXXXX', {'value':'<?php echo $order_total ?>','currency':'GBP'}]);

    I’m trying to replace this part with some thing that will just give the items total:

    <?php echo $order_total ?>

    So are you saying I can put your piece of code in my functions.php… and then replace <?php echo $order_total ?> with something like <?php echo $order_total_without_shipping ?>

    Is that right? Sorry for my lack of understanding, I’m not great with code.

    Yes, that should be right.

    Thread Starter bruceleebee

    (@bennygill)

    I’m receiving this error message when I try add your code to the functions.php file ??

    Fatal error: Cannot redeclare fb_pixeltracking() (previously declared in /home3/ab75688/public_html/wp-content/themes/x-child/functions.php:62) in /home3/ab75688/public_html/wp-content/themes/x-child/functions.php on line 95

    Any ideas what went wrong?

    Thread Starter bruceleebee

    (@bennygill)

    Ok I got it working!

    Well kind of…. the pixel is there and tracking… but it’s still including the shipping price.

    Any ideas what else I could try or tweak to get it working?

    Thread Starter bruceleebee

    (@bennygill)

    Ok I got it working again!! It was just like you said but I forgot to replace the code in both instances. Here is the full code incase anyone else needs it in the future. Thanks again for your help!

    Just replace both instances of XXXXXXXXXXXXXX with your facebook tracking ID, and both instances of USD with your currency.

    // FB Pixel Tracking Code
    
    add_action( 'woocommerce_thankyou', 'fb_pixeltracking' );
    
       function fb_pixeltracking( $order_id ) {
      $order = new WC_Order( $order_id );
      $order_total = $order->get_total();
      $order_total_without_shipping = $order->get_subtotal();
    
     ?>
    
    <!-- Start FB Tracking - Replace XXXXXXXXXXXXXX with your tracking ID to track orders with values-->
    
        <!-- Facebook Conversion Code for Sales from Facebook Ads -->
    
        <script>(function() {
    
         var _fbq = window._fbq || (window._fbq = []);
    
         if (!_fbq.loaded) {
    
           var fbds = document.createElement('script');
    
           fbds.async = true;
    
           fbds.src = '//connect.facebook.net/en_US/fbds.js';
    
           var s = document.getElementsByTagName('script')[0];
    
           s.parentNode.insertBefore(fbds, s);
    
           _fbq.loaded = true;
    
         }
    
        })();
    
        window._fbq = window._fbq || [];
    
        window._fbq.push(['track', 'XXXXXXXXXXXXXX', {'value':'<?php echo $order_total_without_shipping ?>','currency':'USD'}]);
    
        </script>
    
        <noscript>
    
        <img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/trev=XXXXXXXXXXXXXX&cd[value]=<?php echo $order_total_without_shipping ?>&cd[currency]=USD&noscript=1" />
    
        </noscript>
    
         <!-- END FB Tracking -->
    
    <?php
    
    }
    Thread Starter bruceleebee

    (@bennygill)

    @daigo75 I was wondering if you knew how to edit your code (shown below) so that it will output the product item “profit” as generated by the WooCommerce Cost of Goods Sold plugin? I’m trying to figure it out myself but not having much luck. I would be ever so grateful if you could show me how!

    This plugin: https://www.woothemes.com/products/woocommerce-cost-of-goods/

    Your code below…

    function fb_pixeltracking( $order_id ) {
      $order = new WC_Order( $order_id );
      $order_total = $order->get_total();
      $order_total_without_shipping = $order->get_subtotal();
    }
    Diego

    (@daigo75)

    Unfortunately, I don’t know the Cost of Goods plugin. It should allow to retrieve the product cost, but you should ask its developer how to get it. You can then just make a simple subtraction to get the profit.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘PHP code for Order Total Minus Shipping’ is closed to new replies.