簡體   English   中英

將應用優惠券描述添加到 WooCommerce 管理員 Email 通知

[英]Add Applied Coupon Description to WooCommerce Admin Email notifications

試圖將應用優惠券的描述放入“新訂單”管理員 email 中但未成功

到目前為止,我嘗試使用此代碼放入 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
}

除非它不產生任何錯誤,否則我無法在“新訂單”管理員 email 中看到優惠券描述

有人可以幫助我嗎? 先感謝您

您的代碼中有一些錯誤……請嘗試以下操作:

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

在此處輸入圖像描述

暫無
暫無

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

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