Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 475
Kyon147 Avatar asked Nov 17 '15 11:11

Kyon147


People also ask

How do I get total order in WooCommerce?

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 .

How do I find the current order ID in WooCommerce?

Current method: The current way of accomplishing this is by using this function: $order->get_id(); That should return the order id without "#".

How do I view my orders in WooCommerce?

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.


1 Answers

Try something like this for displaying your order total, if you already have access to the $order object.

<?php echo $order->get_total(); ?>
like image 200
Clément Houde Avatar answered Sep 29 '22 19:09

Clément Houde