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

标签 php wordpress woocommerce cart coupon

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

目前看来是这样

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/

相关文章:

javascript - 由于错误 editor-incorrect-element,CKEditor 4 无法正常工作

php - 在 WooCommerce 订单编辑页面中按 “menu order” 对订单项目进行排序

html - 使用 css 删除摘要元素中的蓝色边框

php - 对 woocommerce 中的价格进行四舍五入

php - 在保留数组的同时使用 PHP 将 XML 转换为 JSON

css - 在主菜单下的标题中添加 Logo

mysql - 如何从 Drupal 数据库中获取 post slug

wordpress - 每个属性的 WooCommerce 库存

php - 仅在 Woocommerce 结帐页面中将文本附加到总价

php - Laravel 5.6 - 在 updateOrCreate 后获取更改的值