Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

php - WooCommerce get order total

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try something like this for displaying your order total

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...