Search code examples
wordpresswoocommerceemail-notifications

Append a measuring unit to the quantity field in WooCommerce email notifications


I'd like to append a unit to the quantity field in the order confirmation email, more precisely in the order items table of that email (email-order-details.php)

See screenshot:

enter image description here


I've tried to add this line of PHP

`<?php echo '<p>VE</p>'; ?>`

into the HTML table in the template file, now my code looks like this:

<div style="margin-bottom: 40px;">
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
        <tr>
            <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?> <?php echo '<p>VE</p>'; ?></th>
            <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        echo wc_get_email_order_items( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            $order,
            array(
                'show_sku'      => $sent_to_admin,
                'show_image'    => false,
                'image_size'    => array( 32, 32 ),
                'plain_text'    => $plain_text,
                'sent_to_admin' => $sent_to_admin,
            )
        );
        ?>
    </tbody>

But my adjustment is not showing in the correct location. Can anyone help out with this and hint me in the right direction?


Solution

  • No need to edit/overwrite template files as you can just use the woocommerce_email_order_item_quantity filter hook

    So you get:

    function filter_woocommerce_email_order_item_quantity( $qty_display, $item ) {
        $qty_display = $qty_display . ' VE';
    
        return $qty_display; 
    }
    add_filter( 'woocommerce_email_order_item_quantity', 'filter_woocommerce_email_order_item_quantity', 10, 2 );
    

    Optional: if you don't want to apply this to all e-mail notifications but target specific ones, you can use:

    // Setting the email_is as a global variable
    function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {           
        $GLOBALS['email_id_str'] = $email->id;
    }
    add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
     
    function filter_woocommerce_email_order_item_quantity( $qty_display, $item ) {
        // Getting the email ID global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_id = isset( $refNameGlobalsVar['email_id_str'] ) ? $refNameGlobalsVar['email_id_str'] : '';
    
        // Targeting specific email. Multiple statuses can be added, separated by a comma
        if ( in_array( $email_id, array( 'new_order' ) ) ) {
            $qty_display = $qty_display . ' VE';
        }
    
        return $qty_display; 
    }
    add_filter( 'woocommerce_email_order_item_quantity', 'filter_woocommerce_email_order_item_quantity', 10, 2 ); 
    

    Also see: How to target other WooCommerce order emails


    Code goes in functions.php file of the active child theme (or active theme).