【问题标题】:Add the Coupon Code names to Woocommerce View Order details and email notifications将优惠券代码名称添加到 Woocommerce 查看订单详细信息和电子邮件通知
【发布时间】:2019-07-02 04:16:37
【问题描述】:

我注意到,在查看订单详细信息和电子邮件确认中,它反映了折扣线,但没有说明实际使用的折扣代码。此外,如果折扣代码是 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 项目时甚至不出现折扣行。

【问题讨论】:

    标签: php wordpress woocommerce orders coupon


    【解决方案1】:

    更新 - 使用零值处理折扣

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

    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 美元优惠券场景的优惠券代码(我们一起破解了客户兑换免费物品但需要优惠券),但这没关系我决定使用我以前的代码为这些特殊类型的代码显示单独的行。如果我将您提交的此代码修改为过滤器 woocommerce_email_order_items_table,您提交的此代码是否也适用于订单确认?
    • @user2337231 我刚刚更新了代码以处理优惠券代码显示,即使折扣金额为零也是如此。该代码还处理显示优惠券代码的电子邮件通知。
    • 嗯...似乎仍然无法正常工作...可能是因为该项目是 0.00 美元或可能与另一个过滤器冲突?截图:link
    • 嗯一定是与函数中的另一段代码冲突......无论如何,我可以使用 $0 项目 + $0 代码场景的替代解决方案,你已经非常有帮助了。最后一个问题,如果我不太麻烦的话:我可以将您的解决方案直接应用于“woocommerce_email_order_items_table”操作吗?
    • @user2337231 我已经重新更新了代码……我已经为你添加了一个特殊的代码版本……试试吧,它应该可以工作。
    猜你喜欢
    • 2021-10-23
    • 2018-05-11
    • 2020-11-24
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多