php WooCommerce 获取订单总数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33755713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:36:46  来源:igfitidea点击:

WooCommerce get order total

phpwordpresswoocommerce

提问by Kyon147

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.

我目前正在尝试获取 WooCommerce 结帐的订单总额,以便可以通过 Google AdWords 转换发送。

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:

出于某种原因,当这是在页面上时 $get_order_total = floatval( preg_replace( '#[^\d.]#', '', $order->get_formatted_order_total() ) ); 它打破了页面并产生此错误:

Call to a member function get_formatted_order_total() on a non-object in /woocommerce/checkout/thankyou.php on line 409

在第 409 行的 /woocommerce/checkout/thankyou.php 中的非对象上调用成员函数 get_formatted_order_total()

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.

我环顾四周,也尝试添加全局 $woocommerce 变量,但没有成功。我们使用的版本是 2.1.12。

Your help would be greatly appreciated.

您的帮助将不胜感激。

回答by Clément Houde

Try something like this for displaying your order total

尝试这样的事情来显示您的订单总额

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