php – 获取woocommerce购物车总金额

我试图对购物车的总价格应用折扣,但我只能按商品基价而不是全价购买.我用Google搜索并在this帖子中找到了
wordpress *:

$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() ) );

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

上一篇:php-在Woocommerce上的产品中设置产品类别术语


下一篇:php-通过外部API在WooCommerce中发布和更新JSON数据