我试图对购物车的总价格应用折扣,但我只能按商品基价而不是全价购买.我用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() ) );
这应该可以解决你的问题.