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

Php 将优惠券代码名称添加到Woocommerce查看订单详细信息和电子邮件通知,php,wordpress,woocommerce,orders,coupon,Php,Wordpress,Woocommerce,Orders,Coupon,我注意到,在查看订单详细信息和电子邮件确认中,它反映了折扣行,但没有说明实际使用的折扣代码。此外,如果折扣代码是$0.00(我们有时有一个$0代码用于特殊跟踪目的),它甚至不会显示代码。我花了一整天的时间试图找到一个解决方案——有人能给我一些指导吗?谢谢 到目前为止,我一直在努力获取实际的优惠券代码: add_action( 'woocommerce_order_details_after_order_table', 'custom_woocommerce_coupon_line' ); fun

我注意到,在查看订单详细信息和电子邮件确认中,它反映了折扣行,但没有说明实际使用的折扣代码。此外,如果折扣代码是$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;
}
显示在客户订单视图上,带有2张应用优惠券:


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

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的优惠券:



代码进入活动子主题(或活动主题)的function.php文件。测试和工作。

这工作!你太棒了。基于您的回答,我为我的项目找到了许多解决方案,即使没有与我接触,您也一直是我的救星:)不幸的是,这仍然没有显示用于$0.00项目+$0优惠券场景的优惠券代码(我们将客户兑换免费项目的能力整合在一起,但需要优惠券),但没关系,我决定使用我以前的代码为这些特殊类型的代码显示一行单独的代码。如果我将您提交的代码修改为商业\电子邮件\订单\项目\表,您提交的代码是否也适用于订单确认?@user2337231我刚刚更新了代码以处理优惠券代码显示,即使折扣金额等于零。该代码还处理电子邮件通知,其中还显示优惠券代码。。。似乎还是不起作用。。。可能是因为该项为$0.00,或者可能与其他筛选器冲突?屏幕截图:Hmmm必须与函数中的另一段代码冲突。。。。不管怎样,我可以为$0项目+$0代码场景使用替代解决方案,您已经非常有帮助了。最后一个问题,如果我不是太麻烦的话:我可以直接将您的解决方案应用到“woocommerce\u email\u order\u items\u table”操作吗?@user2337231我已经重新更新了代码…我为您添加了一个特殊的代码版本…试试看,应该可以。