[英]woocommerce - Get Cart Total as number
我想在我的 woocommerce 插件中获得购物车的总价。
我想将它作为浮点数获取,如下所示:21.00 但我不知道如何获取它。 我的代码输出奇怪的结果,这是我的确切代码:
$total = $woocommerce->cart->get_total();
$total_a = WC()->cart->get_total();
$total1 = $woocommerce->cart->get_total_ex_tax();
$total1_a = WC()->cart->get_total_ex_tax();
$total2 = $woocommerce->cart->get_cart_total();
$total2_a = WC()->cart->get_cart_total();
输出:
0,00 €
0,00 €
0,00 €
0,00 €
21,00 €
21,00 €
如果我从字符串转换为浮点数,结果当然是 0.00。
任何帮助如何以浮点数的形式获取购物车总数?
我有这样的代码并且工作完美:
if ( ! WC()->cart->prices_include_tax ) {
$amount = WC()->cart->cart_contents_total;
} else {
$amount = WC()->cart->cart_contents_total + WC()->cart->tax_total;
}
祝你好运 !
直接访问total
属性,它是公开的:
global $woocommerce;
echo $woocommerce->cart->total;
global $woocommerce;
$amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;
您还可以根据需要将 $amount 转换为浮点值。
global $woocommerce;
$woocommerce->cart->cart_contents_total (Cart total)
$woocommerce->cart->tax_total (tax total)
$woocommerce->cart->shipping_total (shipping total)
试试这个 WC()->cart->cart_contents_total
2020 年与 Woocommerce 4+
$total_cart = WC()->cart->get_displayed_subtotal(); // without taxs and shipping fees
echo $total_cart; // ex: 0.00
最好的方法是使用get_total()
同时传入一个上下文而不是默认的“view”。 当上下文被查看时,价格将被格式化以供显示。 当设置为其他任何内容时,它将传回原始值。
例子:
WC()->cart->get_total( 'raw' );
还值得注意的是$woocommerce
(当然$woocommerce
您已经访问了全局第一个)与WC()
完全相同。 我建议尽可能使用WC()
。
它有一些方法,您不需要从属性中获取它们。
使用: WC()->cart->get_cart_contents_total()
而不是: WC()->cart->cart_contents_total
并使用: WC()->cart->get_taxes_total()
而不是: WC()->cart->tax_total
Woocommerce 4.8 WC()->cart->total
工作正常,虽然我有点担心在没有 getter 帮助的情况下获得这个值,但它现在似乎是最简单的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.