Add shipping class under items in New Order email in WooCommerce?

Loading

The following will display The product shipping class name in Woocommerce “New Order” Email notification:

// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id_str'] = $email->id;
}

// Display Items shipping class name in New Order email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 3 );
function custom_order_item_name( $item_name, $item, $is_visible ) {
    // Targeting email notifications only
    if( is_wc_endpoint_url() ) return $item_name;

    // Get the WC_Product object (from order item)
    $product = $item->get_product();

    if( $shipping_class_id = $product->get_shipping_class_id() ){
        // Getting the email ID global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_id = $refNameGlobalsVar['email_id_str'];

        // Only for New Order email notification
        if( ! empty($email_id) && 'new_order' === $email_id ) {
            $shipping_class_name = get_term( $shipping_class_id, 'product_shipping_class' )->name;
            $item_name .= '<br><p class="item-shipping_class" style="margin:12px 0 0;">
            <strong>' . __( 'Shipping class', 'woocommerce' ) . ': </strong>' . $shipping_class_name . '</p>';
        }
    }

    return $item_name;
}

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

About Post Author