Change Woocommerce email subject if order have this specific category

前提是你 提交于 2019-12-23 21:58:58

问题


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.


回答1:


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;
}

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;
}

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




回答2:


Use this:

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;
}


来源:https://stackoverflow.com/questions/49276342/change-woocommerce-email-subject-if-order-have-this-specific-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!