获取woocommerce购物车总金额 [英] Get woocommerce carts total amount
问题描述
我正在尝试对购物车的总价施加折扣,但是我只能对商品的基本价格而不是总价进行折扣.我在Google上搜索了一下,发现其中的此帖子 wordpress stackoverflow:
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 this post in the wordpress stackoverflow:
$ amount = floatval(preg_replace('#[^ \ d.]#','', $ woocommerce->购物车-> get_cart_total())); preg_replace消除了 除十进制字符和冒号外的所有内容.
$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) ); The preg_replace eliminates everything but decimal characters and colons.
如果您想用它做数学运算,则floatval会转换值 从字符串到数字.
Should you care to do math with it, the floatval converts the value from a string to a numeric one.
我尝试添加:
$amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
并更改
$discount = round( (($discounting_amount / 100 ) * $this->amount)*-1, WC()->cart->dp);
到
$discount = round( (($discounting_amount / 100 ) * $amount2)*-1, WC()->cart->dp);
但是出现以下错误:
Fatal error: Call to a member function get_cart_total() on a non-object in...
推荐答案
您需要调用全局变量以确保其获取正确的值.
You need to call the global variable to ensure that it gets the correct values.
如果添加
global $woocommerce;
就在
$amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
应该可以解决您的问题.
that should solve your problem.
这篇关于获取woocommerce购物车总金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!