• Resolved allstarsft

    (@allstarsft)


    Hi!

    I have 3 fields that are custom added one way or another (Custom coded SRP, or Shipping Class and Warranty (Attribute) from Woocommerce but are initially not shown at alll in Cart / Checkout pages.

    I have these 3 field successfully added to Cart / Checkout pages, but am facing the problem of how to add the same to Order Emails.

    Please see below screenshot for sample of the Cart / Check out page with the fields in each product.

    I do not need the Stock number to be in the Emails, just the 3 fields stated above. All fields should appear in the Product details — for each product — and follow the order it is in “here” — Cart / Checkout page.

    Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter allstarsft

    (@allstarsft)

    ** Managed to adjust the fields in Cart / Checkout page — this is the actual order I want the Order emails to have as well. Not sure which template I should edit?

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hello @allstarsft

    To add custom fields to order emails, you need to modify the email templates. These templates are located in your WooCommerce plugin directory under /templates/emails/.

    The email templates you would need to edit are:

    • email-order-details.php: This is used in all order emails and contains the order details table.
    • email-order-items.php: This is used to output individual items in the order.

    In these templates, you can add your custom fields using the available hooks. For instance, to add the fields after the product name, you can use the woocommerce_order_item_meta_end hook.

    I found some resources online that might be helpful to you in achieving your goal:

    Alternatively, you can use an additional plugin like Email Customizer for WooCommerce, which allows you to customize emails with a drag and drop email builder.

    I hope this helps! Let us know if you have any other questions.

    Thread Starter allstarsft

    (@allstarsft)

    @shameemreza

    Thanks for your input on this!

    Seems the Product Meta method should be this link : https://iconicwp.com/docs/woocommerce-custom-fields-for-variations/how-to-add-custom-fields-to-email/

    But above link is for Shops with Variation products.

    IF I’m not wrong, the way I do it is by following this Stackoverflow post –https://stackoverflow.com/questions/63838330/how-to-show-woocommerce-custom-product-meta-in-new-order-emails

    I managed to add SRP custom field into the Order Emails:

    /** Add SRP field into Order Emails - Product Table **/
    add_action( 'woocommerce_order_item_meta_start', 'add_custom_order_meta_srp_to_email', 10, 3 );
    function add_custom_order_meta_srp_to_email($item_id, $item, $order){
       // On email notifications for line items
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            $srp = get_post_meta( $item->get_product_id(), '_srp', true );
    
            if ( ! empty($srp) ) {
                printf( '<div>' .__( "SRP : " . get_woocommerce_currency_symbol() . number_format( round($srp, 2), 2 ) ." / piece.","woocommerce"). '</div>' );
            }
        }
    }	

    Documenting this down for others as well.

    I am also going to add these fields later on :

    1) Shipping Class (product_shipping_class, $shipping_class) and

    2) Attribute – Warranty information (pa_warranty, $warranty).

    Will try out later then update here as well.

    • This reply was modified 1 year, 4 months ago by allstarsft.
    • This reply was modified 1 year, 4 months ago by allstarsft.
    Thread Starter allstarsft

    (@allstarsft)

    Can any expert help me on this ? I had tried the Shipping Class coding but it didn’t work… haha.

    Coding I used :

    /** Add Shipping Class field into Order Emails - Product Table **/
    add_action( 'woocommerce_order_item_meta_start', 'add_custom_order_meta_shipping_class_to_email', 10, 3 );
    function add_custom_order_meta_shipping_class_to_email($item_id, $item, $order){
       // On email notifications for line items
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            $shipping_class = get_post_meta( $item->get_product_id(), 'product_shipping_class', true );
    		
            if ( ! empty($shipping_class) ) {      
    			printf( '<div>' . __("Shipping Class : %s ", "woocommerce"). '</div>', $shipping_class ); 			
    	    }
        }
    }	
    
    Thread Starter allstarsft

    (@allstarsft)

    I think I managed to solve it with this : (not sure why the above — doesn’t work?)

    https://stackoverflow.com/questions/54258286/add-shipping-class-under-items-in-new-order-email-in-woocommerce

    /** Add Shipping Class field into Order Emails - Product Table **/
    
    
    // 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 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 All Order emails notification
            if( ! empty($email_id) )  { //&& 'new_order' === $email_id -- for New Order Email only 
                $shipping_class_name = get_term( $shipping_class_id, 'product_shipping_class' )->name;
                $item_name .= '<p class="item-shipping_class" style="margin:12px 10px; font-weight:600; color: #D5024F"> ' . __( 'Shipping class', 'woocommerce' ) . ': ' . $shipping_class_name . '</p>';
            }
        }
    
        return $item_name;
    }
    Thread Starter allstarsft

    (@allstarsft)

    For PA_warranty, this is what I did — learning from Stackoverflow :

    https://stackoverflow.com/questions/56240214/display-specific-product-attribute-in-woocommerce-email-notifications

    /** Add Attribute - Warranty field into Order Emails - Product Table **/
    add_action( 'woocommerce_order_item_meta_start', 'add_attribute_warranty_to_order_emails', 10, 3 );
    function add_attribute_warranty_to_order_emails( $item_id, $item, $order ) {
        // Set below your product attribute taxonomy (always starts with "pa_")
        $taxonomy = 'pa_warranty';
    
        // On email notifications
        if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
            $product    = $item->get_product();
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
    
            if ( $term_names = $product->get_attribute( $taxonomy ) ) {
                echo '<p class="item-attribute_warranty" style="margin:12px 10px; font-weight:600; color: darkblue">' . $label_name . ': ' . nl2br( $term_names ) . '</p>';
            }
        }
    }

    Though I managed to solve all three …. but there’s a minor issue :

    In Cart / Checkout page — the order of the 3 fields are as follows :

    1. Warranty
    2. Shipping Class
    3. SRP

    But in the Order Emails, Shipping class became first. No matter I changed the numbers of the priority — its still the same. — add_filter( ‘woocommerce_order_item_name’, ‘shipping_class_order_item_name’, 10, 3 );

    #Scratch head….

    Thread Starter allstarsft

    (@allstarsft)

    Update — Shipping Class. I found another way to code.. the stackoverflow version is abit complicated for me to understand how its done..

    /** Add Shipping Class field into Order Emails - Product Table **/
    // Display Items shipping class name in Order email notification
    add_filter( 'woocommerce_order_item_name', 'shipping_class_order_item_name', 20, 3 );
    function shipping_class_order_item_name( $item_name, $item, $is_visible ) {
        // Targeting email notifications only
        if( is_wc_endpoint_url() ) { 
    		return $item_name;
    	}
    
        $product = $item->get_product();     // Get the WC_Product object (from order item)
    	$shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
    	$shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );
    	
        if( empty( $shipping_class_id ) ) {
            return $item_name; // Return default product title (in case of)
    	}
    
        $label = __( 'Shipping class', 'woocommerce' );
    	
    	// Output the Product Title and the new code which wraps the Shipping Class name
        return $item_name . ' <p class="item-shipping_class" style="margin:12px 10px; color: #D5024F;"><strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
    }
    	

    So currently the priority for — add_filter( ‘woocommerce_order_item_name’, ‘shipping_class_order_item_name’, 20, 3 );

    At 20 it should be appearing after Warranty — which is 10

    add_action( ‘woocommerce_order_item_meta_start’, ‘add_attribute_warranty_to_order_emails’, 10, 3 );

    But in Email preview, its still showing as Shipping Class first — am using Kadence Email Customizer for the Preview.

    Mirko P.

    (@rainfallnixfig)

    Hi @allstarsft 👋

    Thank you for getting back to us on the WooCommerce forum.

    Support for customizations is limited on this forum as we’re focusing more on the default WooCommerce core features.

    If you need more help with this moving forward, I can also recommend the following resources for help:

    That said, I’m going to leave the thread open for a bit to see if anyone is able to chime in to help you out.

    Cheers.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Order Emails – Adding Custom fields for each product’ is closed to new replies.