WooCommerce get order total

2019-01-23 18:19发布

问题:

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; ?>&amp;currency_code=GBP&amp;label=CzWXCLmwn1YQ_aLJ_AM&amp;guid=ON&amp;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.

回答1:

Try something like this for displaying your order total

<?php echo $order->get_total(); ?>