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

问题描述

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

到目前为止尝试使用我放入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 文件中。经测试有效。

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...