php 获取woocommerce购物车总金额

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22249615/
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 04:45:52  来源:igfitidea点击:

Get woocommerce carts total amount

phpwordpresswoocommercediscountcoupon

提问by Howli

I am trying to apply a discount to a carts total price, but I can only do it to the item base price and not the over all price. I Googled and came across thispost in the wordpress stackoverflow:

我正在尝试对购物车总价应用折扣,但我只能对商品基本价格进行折扣,而不能对总价进行折扣。我用谷歌搜索并在 wordpress stackoverflow 中看到了这篇文章:

$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) ); The preg_replace eliminates everything but decimal characters and colons.

Should you care to do math with it, the floatval converts the value from a string to a numeric one.

$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ); preg_replace 消除了除十进制字符和冒号之外的所有内容。

如果您想用它进行数学运算,floatval 会将值从字符串转换为数字值。

I tried adding:

我尝试添加:

$amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );

and changing

和改变

$discount = round( (($discounting_amount / 100 ) *  $this->amount)*-1, WC()->cart->dp);

to

$discount = round( (($discounting_amount / 100 ) *  $amount2)*-1, WC()->cart->dp);

But I get the following error:

但我收到以下错误:

Fatal error: Call to a member function get_cart_total() on a non-object in...

回答by zennin

Try this:

尝试这个:

WC()->cart->cart_contents_total

The function get_cart_total uses wc_price function thas converts cart_contents_total to currency.

函数get_cart_total 使用wc_price 函数将cart_contents_total 转换为货币。

回答by Ronan

You need to call the global variable to ensure that it gets the correct values.

您需要调用全局变量以确保它获得正确的值。

If you add

如果添加

 global $woocommerce;

just before

就在之前

 $amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );

that should solve your problem.

那应该可以解决您的问题。

回答by colapsnux

This also work nicelly.

这也很好用。

WC()->cart->total

WC()->cart->total

回答by Muvans

This works perfectly and removes currency symbol:

这完美地工作并删除货币符号:

     $woocommerce->cart->total;

回答by Buzut

As of late 2018, the best way is to use get_cart_contents_total(). This is the total of items in the cart after discounts.

截至 2018 年底,最好的方法是使用get_cart_contents_total(). 这是折扣后购物车中的商品总数。

WC()->cart->get_cart_contents_total(); // Float

Other methods are available for more specific needs, just have a look at the docs.

其他方法可用于更具体的需求,只需查看docs

回答by Deepak Kumar

global $woocommerce;
    $amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;

You can also convert $amount in float value as per your requirement.

您还可以根据需要将 $amount 转换为浮点值。

回答by typocoder

To show the carts total including tax and discounts use this

要显示包括税款和折扣在内的购物车总数,请使用此

$ordertotal = wp_kses_data( WC()->cart->get_total() );
$ordertotal = wp_kses_data( WC()->cart->get_total() );

回答by mital korat

$totalamount = $woocommerce->cart->cart_contents_total;

$totalamount = $woocommerce->cart->cart_contents_total;

echo $totalamount;

回声 $totalamount;

回答by Geza Gog

Function get_cart_contents_total() gives the total of items in the cart, but after discounts. Depending on the tax settings you might have to add taxes. Something lie this:

函数 get_cart_contents_total() 给出购物车中的商品总数,但在折扣后。根据税收设置,您可能需要加税。有一点是这样的:

$cart_total_price = wc_prices_include_tax() ? WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax() : WC()->cart->get_cart_contents_total();