嗨,伙计们,我想展示一下客户通过当前交易节省了多少钱。我输入了我店里所有产品的正常价格和销售价格,它显示了prdocut目录上的折扣百分比。Woocommerce购物车仅显示总计和小计。
小计:10美元总计:10美元$
所以我写了这个函数来显示我想要什么。虽然它可以在我的本地主机上运行,但我无法在实时主机上获得任何结果。
function discount_module_in() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$price=get_post_meta($values[\'product_id\'] , \'_regular_price\', true);
$sale=get_post_meta($values[\'product_id\'] , \'_sale_price\', true);
$amount=$price-$sale;
$discount=(1-$sale/$price)*100;
if (intval($discount)==70) {
$amount70+=$amount;
}
if (intval($discount)==50) {
$amount50+=$amount;
}
if (intval($discount)==30) {
$amount30+=$amount;
}
}
if ($amount70>0) {
echo "70% OFF: -".$amount70.get_woocommerce_currency_symbol()."<br>";
}
if ($amount50>0) {
echo "50% OFF: -".$amount50.get_woocommerce_currency_symbol()."<br>";
}
if ($amount30>0) {
echo "30% OFF: -".$amount30.get_woocommerce_currency_symbol()."<br>";
}
}
add_action( \'woocommerce_after_cart_totals\', \'discount_module_in\');
我还检查了它的主题是否存在问题,但我只是将代码粘贴到函数中。php,以确保我尝试了简单的函数,并且它工作正常。这也不是woocommerce挂钩问题。
function discount_module_in() {
echo "test"
}
add_action( \'woocommerce_after_cart_totals\', \'discount_module_in\');
我也用这个来更改代码。它在本地主机上运行,但在实时主机上仍不工作。
function deneme(){
global $woocommerce;
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item ){
$product = wc_get_product( $cart_item[\'product_id\'] );
$price=$product->get_regular_price();
$sale=$product->get_sale_price();
$amount=$price-$sale;
$discount=(1-$sale/$price)*100;
if (intval($discount)==70) {
$amount70+=$amount;
}
if (intval($discount)==50) {
$amount50+=$amount;
}
if (intval($discount)==30) {
$amount30+=$amount;
}
}
if ($amount70>0) {
echo "70% OFF: -".$amount70.get_woocommerce_currency_symbol()."<br>";
}
if ($amount50>0) {
echo "50% OFF: -".$amount50.get_woocommerce_currency_symbol()."<br>";
}
if ($amount30>0) {
echo "30% OFF: -".$amount30.get_woocommerce_currency_symbol()."<br>";
}
}
add_action( \'woocommerce_proceed_to_checkout\', \'deneme\');
调试后,它给了我这个错误
警告:在/home/u693223509/domains/inuovo中遇到非数字值。com/public\\u html/wp-content/themes/inuovo/functions。php在线34
警告:在/home/u693223509/domains/inuovo中遇到非数字值。com/public\\u html/wp-content/themes/inuovo/functions。php在线34
警告:在/home/u693223509/domains/inuovo中遇到非数字值。com/public\\u html/wp-content/themes/inuovo/functions。php在线35
警告:在/home/u693223509/domains/inuovo中遇到非数字值。com/public\\u html/wp-content/themes/inuovo/functions。php在线35
警告:在/home/u693223509/domains/inuovo中被零除。com/public\\u html/wp-content/themes/inuovo/functions。php在线35
这是第34行和第35行
$amount=$price-$sale;
$discount=(1-$sale/$price)*100;