簡體   English   中英

將優惠券代碼名稱添加到 Woocommerce 查看訂單詳細信息和電子郵件通知

[英]Add the Coupon Code names to Woocommerce View Order details and email notifications

我注意到在查看訂單詳細信息和電子郵件確認中,它反映了一個折扣行,但沒有說明實際使用的折扣代碼。 此外,如果折扣代碼是 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 項目時,甚至不出現 Discount 行。

更新-處理零價值的折扣

以下代碼將在訂單總計行中的“折扣”行之后,顯示應用到訂單的優惠券:

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 文件中。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM