如果订单具有此特定类别,请更改 Woocommerce 电子邮件主题 [英] Change Woocommerce email subject if order have this specific category

查看:94
本文介绍了如果订单具有此特定类别,请更改 Woocommerce 电子邮件主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道如果订单具有特定类别(如(预购)),是否可以更改电子邮件主题.我想将 PO 放在开头(PO 新客户订单 #0000),然后客户收到的所有其他订单都会收到默认电子邮件主题(新客户订单 #0000).

I just want to know if possible to change the email subject if the order have the specific category like (Preorder). I want to put PO at beginning (PO New customer order #0000) then all other order the customer receive the default email subject (New Customer Order #0000).

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

    global $woocommerce;
    global $product;       
    if ( has_term( 'preorder', $product->ID ) ) {           
        $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;
}

注意:我只是在某处复制了这段代码.

Note: I just copy this code somewhere.

推荐答案

这个可以这样做,做一些小改动:

This can be done this way, making some small changes:

add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);
function custom_admin_email_subject( $subject, $order ) {
    $backordered = false;

    foreach($order->get_items() as $item_id => $item ){
        if ( has_term( 'preorder', 'product_cat' , $item->get_product_id() ) ) { 
            $backordered = true;
            break;
        }
    } 
    if ( $backordered ) {  
        $subject = sprintf( '[PO]New customer order (# %s) from %s %s', $order->get_id(), $order->get_billing_first_name(), $order->get_billing_last_name() );
    } 
    return $subject;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of the active child theme (or active theme). Tested and works.

或者可以在没有产品类别的情况下以这种方式完成,检查产品是否缺货:

Or it can be done this way without a product category, checking that product is backordered:

add_filter('woocommerce_email_subject_new_order', 'custom_admin_email_subject', 1, 2);
function custom_admin_email_subject( $subject, $order ) {
    $backordered = false;

    foreach($order->get_items() as $item_id => $item ){
        $product = $item->get_product();
        if( $product->get_backorders() == 'yes' && $product->get_stock_quantity() < 0 ){
            $backordered = true;
            break;
        }
    }
    if ( $backordered ) {
        $subject = sprintf( '[PO]New customer order (# %s) from %s %s', $order->get_id(), $order->get_billing_first_name(), $order->get_billing_last_name() );
    }
    return $subject;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of the active child theme (or active theme). Tested and works.

这篇关于如果订单具有此特定类别,请更改 Woocommerce 电子邮件主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆