显示Woocommerce电子邮件主题中订单商品的产品属性值

时间:2018-09-19 23:36:43

标签: php wordpress woocommerce hook-woocommerce custom-taxonomy

在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是我的属性的子弹)

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电子邮件主题的订单项中获取并显示特定的产品属性值?

1 个答案:

答案 0 :(得分:1)

订单可以包含很多商品,自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文件中。经过测试,可以正常工作。

相关问题