簡體   English   中英

顯示Woocommerce電子郵件主題中訂單商品的產品屬性值

[英]Display product attribute value from order item in Woocommerce email subject

在woocommerce中,我試圖獲取特定的產品屬性值並將其顯示在主題行中,以用於管理新訂單電子郵件通知。

我找到了以下代碼,但是我缺乏使它起作用的知識:

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {

    global $woocommerce;
    global $product;       
    {
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
        $subject = sprintf( '[%s] New customer order (# %s) from %s %s',
                               $blogname, $order->id,
                               $order->billing_first_name, $order->billing_last_name );
    } 
    return $subject;
}

我也嘗試了這個xxxxx是我的屬性的xxxxx

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {

    global $woocommerce;
    global $product;       
    {  
        $pa_xxxxx_value = get_order_meta($order->id, 'pa_xxxxx', true);
        $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
        $subject = sprintf( '[%s] [%s] New customer order (# %s) from %s %s',
                              $pa_xxxxx_value, $blogname, $order->id,
                              $order->billing_first_name, $order->billing_last_name );
    } 
    return $subject;
}

但這不是有效的手段。

如何從Woocommerce電子郵件主題中的訂單商品獲取並顯示特定的產品屬性值?

訂單可以包含很多物品,並且自Woocommerce 3起,您的代碼中就有一些錯誤。

以下代碼將在訂單項中搜索特定的產品屬性(分類法),如果找到,它將顯示具有該產品屬性術語名稱值的新自定義主題:

add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
function change_admin_email_subject( $subject, $order ) {
    // HERE define the product attribute taxonomy (start always with "pa_")
    $taxonomy = 'pa_color'; //

    // Loop through order items searching for the product attribute defined taxonomy
    foreach( $order->get_items() as $item ){
        // If product attribute is found
        if( $item->get_meta($taxonomy) ){
            // Custom new subject including the product attribute term name
            $subject = sprintf( '[%s] [%s] New customer order (# %s) from %s %s',
                get_term_by('slug', $item->get_meta($taxonomy), $taxonomy )->name, // Term name
                wp_specialchars_decode(get_option('blogname'), ENT_QUOTES),
                $order->get_id(),
                $order->get_billing_first_name(),
                $order->get_billing_last_name()
            );
            break; // Stop the loop
        }
    }

    return $subject;
}

代碼進入您的活動子主題(或活動主題)的function.php文件中。 經過測試和工作。

暫無
暫無

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

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