gpt4 book ai didi

php - 在 Woocommerce 中使用优惠券打折时显示购物车项目小计

转载 作者:行者123 更新时间:2023-12-03 23:14:28 24 4
gpt4 key购买 nike

我正在尝试为购物车商品添加优惠券折扣,我可以这样做,但问题是我不想突出没有优惠券折扣的商品。

目前看来是这样

enter image description here

但我希望它看起来像这样。

enter image description here

简单来说,我只想突出应用了折扣/优惠券折扣的购物车商品,而其他商品价格保持不变。

这是我当前在 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;
}

最佳答案

在购物车中已经有一些相关的功能可以帮助优惠券折扣,这将简化您正在尝试做的事情。共有 2 件购物车商品:

  • 'line_subtotal' 是非折扣购物车项目行总计
  • 'line_total' 是打折的购物车商品行总计

所以不需要外部函数,也不需要检测优惠券是否被应用。因此功能代码将非常紧凑以获得所需的显示:

add_filter( 'woocommerce_cart_item_subtotal', 'show_coupon_item_subtotal_discount', 100, 3 );
function show_coupon_item_subtotal_discount( $subtotal, $cart_item, $cart_item_key ){
if( $cart_item['line_subtotal'] !== $cart_item['line_total'] ) {
$subtotal = sprintf( '<del>%s</del> <ins>%s</ins>', wc_price($cart_item['line_subtotal']), wc_price($cart_item['line_total']) );
}
return $subtotal;
}

代码进入事件子主题(或事件主题)的 function.php 文件。经过测试并有效。

enter image description here

关于php - 在 Woocommerce 中使用优惠券打折时显示购物车项目小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53689604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com