gpt4 book ai didi

php - 将优惠券代码名称添加到 Woocommerce 查看订单详细信息和电子邮件通知

转载 作者:行者123 更新时间:2023-12-02 00:04:50 24 4
gpt4 key购买 nike

我注意到,在查看订单详细信息和电子邮件确认中,它反射(reflect)了折扣线,但没有说明实际使用的折扣代码。此外,如果折扣代码为 0.00 美元(我们有时出于特殊跟踪目的而使用 0 美元代码),它甚至根本不会显示该代码。我花了一整天的时间试图找到解决方案 - 有人可以对此提供一些指导吗?谢谢。

到目前为止,我已经成功获取实际的优惠券代码:

add_action( 'woocommerce_order_details_after_order_table', 'custom_woocommerce_coupon_line' );
function custom_woocommerce_coupon_line( $order_id ) {
$order = wc_get_order( $order_id );

// An order can have no used coupons or also many used coupons
$coupons = $order->get_used_coupons();
$coupons = count($coupons) > 0 ? implode(',', $coupons) : '';
echo $coupons;
}

但不知道如何将其放入“折扣”行...也不知道为什么当它是使用 $0 代码的 0 美元商品时折扣行甚至不出现。

最佳答案

更新 - 处理零值折扣

以下代码将在订单总计行中的“折扣”行之后,显示订单所应用的优惠券:

add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
// Exit if there is no coupons applied
if( sizeof( $order->get_used_coupons() ) == 0 )
return $total_rows;

$new_total_rows = []; // Initializing

foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;

if( $key == 'discount' ){
// Get applied coupons
$applied_coupons = $order->get_used_coupons();
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons ),
);
}
}

return $new_total_rows;
}

显示在客户订单 View 上,并使用 2 张优惠券:

enter image description here

<小时/>

附加代码版本:处理零折扣金额的已应用优惠券,请改用此代码:

add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
$has_used_coupons = sizeof( $order->get_used_coupons() ) > 0 ? true : false;

// Exit if there is no coupons applied
if( ! $has_used_coupons )
return $total_rows;

$new_total_rows = []; // Initializing
$applied_coupons = $order->get_used_coupons(); // Get applied coupons

foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;

// Adding the discount line for orders with applied coupons and zero discount amount
if( ! isset($total_rows['discount']) && $key === 'shipping' ) {
$new_total_rows['discount'] = array(
'label' => __( 'Discount:', 'woocommerce' ),
'value' => wc_price(0),
);
}

// Adding applied coupon codes line
if( $key === 'discount' || isset($new_total_rows['discount']) ){
// Get applied coupons
$applied_coupons = $order->get_used_coupons();
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons ),
);
}
}

return $new_total_rows;
}

在电子邮件通知中显示具有 0 折扣的优惠券:

enter image description here

<小时/>

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

关于php - 将优惠券代码名称添加到 Woocommerce 查看订单详细信息和电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54583645/

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