title: Woocommerce facebook new pixel tracking code add purchase into value
body: <p>I have been working on making the new facebook pixel tracking code function on my woocommerce site. I have a couple things I want to do with this, and I've started where I figured was the easiest place. Firstly, I want to change this code to use the checkout subtotal instead of the order total, because I use a deposit, which changes the order total to 20% of the actual price. Secondly, I use a pixel tracking plugin, and I'd like to add this function into the plugin to be able to quickly insert it into other pages. I've successfully been able to get the code to work by adding this to functions.php:</p>
  
  <pre><code>// Add purchase code to order received page
  
  function checkout_analytics( $order_id ) {
      $order = new WC_Order( $order_id );
      $currency = $order-&gt;get_order_currency();
      $total = $order-&gt;get_total();
      $date = $order-&gt;order_date;
      ?&gt;
  &lt;!-- Facebook Pixel Code --&gt;
  &lt;script&gt;
  !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
  n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
  t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
  document,'script','https://connect.facebook.net/en_US/fbevents.js');
  fbq('init', 'XXXXXXXXXX'); // Insert your pixel ID here.
  &lt;/script&gt;
  &lt;script&gt;
  fbq('track', 'Purchase', {
  value: &lt;?php echo $order-&gt;get_total(); ?&gt;,
  currency: 'USD'
  });
  &lt;/script&gt;
  &lt;noscript&gt;&lt;img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=XXXXXXXXXX&amp;ev=PageView&amp;noscript=1"
  /&gt;&lt;/noscript&gt;
  &lt;!-- DO NOT MODIFY --&gt;
  &lt;!-- End Facebook Pixel Code --&gt;
      &lt;?php   
  }
  add_action( 'woocommerce_thankyou', 'checkout_analytics' );
  </code></pre>
  
  <p>So to try and get the order subtotal I've changed the code like this, which doesn't work. It doesn't pull the value of the subtotal, and I think I've got some formatting issues here. <a href="https://i.stack.imgur.com/oWDD5.jpg" rel="nofollow noreferrer">Pixel error picture</a></p>
  
  <pre><code>// Add purchase code to order received page
  
  function checkout_analytics( $order_id ) {
      $order = new WC_Order( $order_id );
      $currency = $order-&gt;get_order_currency();
      $total = $order-&gt;get_total();
      $date = $order-&gt;order_date;
      $cart_subtotal = $order-&gt;subtotal_ex_tax;
      ?&gt;
  &lt;!-- Facebook Pixel Code --&gt;
  &lt;script&gt;
  !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
  n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
  t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
  document,'script','https://connect.facebook.net/en_US/fbevents.js');
  fbq('init', 'XXXXXXXXXXXXXX'); // Insert your pixel ID here.
  &lt;/script&gt;
  &lt;script&gt;
  fbq('track', 'Purchase', {
  value: &lt;?php echo $order-&gt;subtotal_ex_tax; ?&gt;,
  currency: 'USD'
  });
  &lt;/script&gt;
  &lt;noscript&gt;&lt;img height="1" width="1" style="display:none"
  src="https://www.facebook.com/tr?id=XXXXXXXXX&amp;ev=PageView&amp;noscript=1"
  /&gt;&lt;/noscript&gt;
  &lt;!-- DO NOT MODIFY --&gt;
  &lt;!-- End Facebook Pixel Code --&gt;
      &lt;?php   
  }
  add_action( 'woocommerce_thankyou', 'checkout_analytics' );
  </code></pre>
  
  <p>I'd like to get the code working properly, then I want to add this into a plugin, but I'm not sure how to do that entirely. My vague thought is to copy the function, but I feel as though that's not going to entirely work.</p>