phpwoocommerceproductordersemail-notifications

Add Product Tags to order items on WooCommerce email notifications


With the help of this forum I have managed to add a custom field to the woocommerce order emails using this code snippet:

`/**
* Add a custom field & product tag (in an order) to the emails
*/
add_action('woocommerce_order_item_meta_end', 'email_confirmation_display_order_items', 10, 4);

function email_confirmation_display_order_items($item_id, $item, $order, $plain_text) {

   $product = $item->get_product();

   if ( empty( $product ) ) {
    return false;
   }

   $date = get_field( 'course_dates', $product->get_id() );

   if ( !empty( $date ) ) {
    echo '<div style="font-size:12px;"><br>Course Date(s): '. $date .'</div>';
}

 }

I would now like to improve upon that and also add 'product tag' to those emails (I am using the product tag to specify the location of events).

Having looked around the forum and played around a little I have come up with the following which is clearly inorrect as it breaks the site:

add_action( 'woocommerce_order_item_meta_end', 'email_confirmation_display_product_tag', 10, 4 );
function email_confirmation_display_product_tag($order, $sent_to_admin, $plain_text, $email) {
   $items = $order->get_items();
   foreach ( $items as $item ) { 
    $product_tag = $item['product_tag'];
   }    
   if ( !empty($product_tag ) ) {
    echo '<div style="font-size:12px;">Location: '. $product_tag .'</div>';
}
}

Any assitance here to get me on the right path would be greatly appreciated.


Solution

  • There are some mistakes in your code, try the following instead:

    add_action( 'woocommerce_order_item_meta_end', 'email_confirmation_display_product_tag', 10, 4 );
    function email_confirmation_display_product_tag($item_id, $item, $order, $plain_text) {
        // Only on email notifications and "line" items
        if( is_wc_endpoint_url() || ! $item->is_type('line_item') ) return;
    
        // Get the product tag(s) for this order item
        $term_names = wp_get_post_terms( $item->get_product_id(), 'product_tag', array('fields' => 'names') );  
    
        // Display
        if ( count($term_names ) > 0 ) {
            printf( '<div style="font-size:12px;"><br>%s: %s</div>', __('Location', 'woocommerce'), reset($term_names) );
        }
    }
    

    It should work.


    You should merge both functions in one as they use the same hook, like:

    add_action( 'woocommerce_order_item_meta_end', 'email_confirmation_display_custom_data', 20, 4 );
    function email_confirmation_display_custom_data($item_id, $item, $order, $plain_text) {
        // Only on email notifications and "line" items
        if( is_wc_endpoint_url() || ! $item->is_type('line_item') ) return;
    
        $product = $item->get_product(); // get the WC_Product object
    
        // Get the ACF 'course_dates' field value
        if ( $date = get_field( 'course_dates', $product->get_id() ) ) {
            printf( '<div style="font-size:12px;"><br>%s: %s</div>', __('Course Date(s)', 'woocommerce'), $date );
        }
    
        // Get the product tag(s) for this order item
        $term_names = wp_get_post_terms( $item->get_product_id(), 'product_tag', array('fields' => 'names') );  
    
        // Display
        if ( count($term_names ) > 0 ) {
            printf( '<div style="font-size:12px;"><br>%s: %s</div>', __('Location', 'woocommerce'), reset($term_names) );
        }
    }
    

    Be sure to remove other related functions before. It should work.