将应用的优惠券描述添加到 WooCommerce 管理员电子邮件通知

Add Applied Coupon Description to WooCommerce Admin Email notifications

尝试将已申请优惠券的描述放入“新订单”管理员电子邮件但未成功

到目前为止,我一直在尝试使用我放入 functions.php

中的代码
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {

    // Only for admins and when there at least 1 coupon in the order
    if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;

    foreach( $order->get_items('coupon') as $coupon ){
        $coupon_codes[] = $coupon->get_code();
        $coupon_desc[] = $coupon->get_description();

    }
    // For one coupon
    if( count($coupon_codes) == 1 ){
        $coupon_code = reset($coupon_codes);
        echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';

        $coupon_des = reset($coupon_desc);
        echo '<p>'.__( 'Coupon Description: ').$coupon_des.'<p>';
    } 
    // For multiple coupons
}

除非它不产生任何错误,否则我无法在“新订单”管理员电子邮件中看到优惠券描述

有人可以帮助我吗?提前谢谢你

您的代码中存在一些错误……请尝试以下操作:

add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for admins and when there at least 1 coupon in the order
    if ( $sent_to_admin && count( $order->get_coupons() ) > 0 ) {
        $html = '<div class="coupon-items">
        <h2>' . __( "Used coupons" ) . '<h2>
        <table class="td" cellspacing="0" cellpadding="6" border="1"><tr>
        <th>' . __("Code") . '</th>
        <th>' . __("Description") . '</th>
        </tr>';

        foreach( $order->get_coupons() as $item ){
            $code   = $item->get_code();
            $coupon = new WC_Coupon($code);

            $html .= '<tr>
                <td>' . ucfirst( $code ) . '</td>
                <td>' . $coupon->get_description() . '</td>
            </tr>';
        }
        $html .= '</table><br></div>';

        // The CSS styling
        $css = '<style>
            .coupon-items table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
                color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
            .coupon-items table th, table.tracking-info td{text-align: left; border-top-width: 4px;
                color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
            .coupon-items table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
        </style>';

        // The Output CSS + HTML
        echo $css . $html;
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。