我试图在购物车项目上添加优惠券折扣,我能够做到这一点,但问题是我不想伸出没有优惠券折扣项目的项目 .
目前,它看起来像这样
但我希望它看起来像这样 .
简单来说,我希望只使用已经应用折扣/优惠券折扣的购物车项目以及其他商品价格保持不变 .
这是我在function.php中的当前代码
add_filter( 'woocommerce_cart_item_subtotal', 'show_coupon_item_subtotal_discount', 99, 3 );
function show_coupon_item_subtotal_discount( $subtotal, $cart_item, $cart_item_key ){
global $woocommerce;
if ( $woocommerce->cart->has_discount( $coupon_code )) {
$newsubtotal = wc_price( woo_type_of_discount( $cart_item['line_total'], $coupon->discount_type, $coupon->amount ) );
$subtotal = sprintf( '<s>%s</s> %s', $subtotal, $newsubtotal );
}
return $subtotal;
}
function woo_type_of_discount( $price, $type, $amount ){
switch( $type ){
case 'percent_product':
$newprice = $price * ( 1 - $amount/100 );
break;
case 'fixed_product':
$newprice = $price - $amount;
break;
case 'percent_cart':
$newprice = $price * ( 1 - $amount/100 );
break;
case 'fixed_cart':
$newprice = $price - $amount;
break;
default:
$newprice = $price;
}
return $newprice;
}
1 回答
在购物车中已经有一些相关功能可以帮助优惠券折扣,这将简化您的尝试 . 有2个购物车项目总数:
'line_subtotal'是非折扣购物车项目总计
'line_total'是折扣购物车项目总计
因此,不需要外部功能,也无需检测是否应用优惠券 . 因此,功能代码将非常紧凑,以获得所需的显示:
代码位于活动子主题(或活动主题)的function.php文件中 . 经过测试和工作 .