I am currently trying to get the order total of a checkout for WooCommerce so it can be sent through with a Google AdWords conversion.
Here is the code:
<?php
$get_order_total = floatval( preg_replace( '#[^\d.]#', '', $order->get_formatted_order_total() ) );
?>
<!-- Google Code for ATS Conversion Page -->
<?php if ( $get_order_total ) { ?>
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 1066553725;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "CzWXCLmwn1YQ_aLJ_AM";
if (<?php echo $get_order_total; ?>) { var google_conversion_value = <?php echo $get_order_total; ?>; var google_conversion_currency = "GBP"; }
var google_conversion_currency = "GBP";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/1066553725/?value=<?php echo $get_order_total; ?>&currency_code=GBP&label=CzWXCLmwn1YQ_aLJ_AM&guid=ON&script=0"/>
</div>
</noscript>
<?php } ?>
For some reason when this is on the page $get_order_total = floatval( preg_replace( '#[^\d.]#', '', $order->get_formatted_order_total() ) ); it breaks the page and produces this error:
Call to a member function get_formatted_order_total() on a non-object in /woocommerce/checkout/thankyou.php on line 409
I have looked around and also tried adding the global $woocommerce variable but with no success. The version we are using is 2.1.12.
Your help would be greatly appreciated.
Exactly, it is right there in the error message. If you have the order ID, you can $order = wc_get_order( $order_id ) to get the order object. Also $order->get_total() might mean you don't need to do all that preg_replace .
Current method: The current way of accomplishing this is by using this function: $order->get_id(); That should return the order id without "#".
Viewing orders You can take orders from the order management page. To check out your orders, you need to go WooCommerce >> Orders from your WooCommerce admin panel. Each order row displays useful details, for example, the email, customer address, phone number, and the order status.
Try something like this for displaying your order total, if you already have access to the $order
object.
<?php echo $order->get_total(); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With