微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 获取woocommerce购物车总金额

我试图对购物车的总价格应用折扣,但我只能按商品基价而不是全价购买.我用Google搜索并在this帖子中找到了
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.

我尝试添加

$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...

解决方法:

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

如果你添加

 global $woocommerce;

就在此之前

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

这应该可以解决你的问题.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐