如果订单具有此特定类别,请更改Woocommerce电子邮件主题

时间:2018-03-14 11:16:28

标签: php wordpress woocommerce orders email-notifications

如果订单具有特定类别(预购),我只想知道是否可以更改电子邮件主题。我想在PO开头(PO新客户订单#0000)然后所有其他订单客户收到默认电子邮件主题(新客户订单#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;
}

注意:我只是在某处复制此代码。

2 个答案:

答案 0 :(得分:1)

使用此:

function change_admin_email_subject( $subject, $order ) {
    // Get all order items
    $items = $order->get_items();
    $found = false;
    // Loop through the items
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        // get the categories for current item
        $terms = get_the_terms( $product_id, 'product_cat' );
        // Loop through the categories to find if 'preorder' exist.
        foreach ($terms as $term) {
            if($term->slug == 'preorder'){
                $subject = 'PO '. $subject;
                $found = true;
                break;
            }
        }
        if($found == true){
            break;
        }
    }
    return $subject;
}

答案 1 :(得分:1)

这可以通过这种方式完成,进行一些小改动:

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文件。经过测试和工作。

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

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文件。经过测试和工作。