Skip to content

Instantly share code, notes, and snippets.

@jan-koch
Last active April 17, 2021 15:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-koch/70e40f51fea0a536053b33b538dda4c1 to your computer and use it in GitHub Desktop.
Save jan-koch/70e40f51fea0a536053b33b538dda4c1 to your computer and use it in GitHub Desktop.
Email order items template from WooCommerce, containing a custom snippet to show a custom SKU for each product - only in admin emails. This file goes into wp-content/themes/your-child-theme/woocommerce/emails.
<?php
/**
* Email Order Items
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates/Emails
* @version 3.4.0
*/
defined( 'ABSPATH' ) || exit;
$text_align = is_rtl() ? 'right' : 'left';
foreach ( $items as $item_id => $item ) :
$product = $item->get_product();
$sku = '';
$purchase_note = '';
$image = '';
if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
continue;
}
if ( is_object( $product ) ) {
$sku = $product->get_sku();
$purchase_note = $product->get_purchase_note();
$image = $product->get_image( $image_size );
}
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;">
<?php
// Show title/image etc.
if ( $show_image ) {
echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );
}
// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );
// SKU.
if ( $show_sku && $sku ) {
echo wp_kses_post( ' (#' . $sku . ')' );
// load the custom SKU
$custom_sku = get_post_meta( $product->get_id(), 'jk_sku', true );
if ( is_string( $custom_sku ) ) { // only show the custom SKU if it's set
echo "<br>" . wp_kses_post( "Custom SKU: $custom_sku" ); // change this line if needed
}
}
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
wc_display_item_meta( $item );
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );
?>
</td>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post( apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ) ); ?>
</td>
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php echo wp_kses_post( $order->get_formatted_line_subtotal( $item ) ); ?>
</td>
</tr>
<?php
if ( $show_purchase_note && $purchase_note ) {
?>
<tr>
<td colspan="3" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
<?php
echo wp_kses_post( wpautop( do_shortcode( $purchase_note ) ) );
?>
</td>
</tr>
<?php
}
?>
<?php endforeach; ?>
@Ariel-76
Copy link

Hello
How to change the code so the customer will se the sku?

@jan-koch
Copy link
Author

Hello
How to change the code so the customer will se the sku?

That depends on where you want the SKU to show. In this example, you'd edit line 57 as follows:

echo "<br>" . wp_kses_post( "SKU: $sku" );

The SKU is stored in the variable $sku.

Best

@uebes
Copy link

uebes commented Mar 7, 2020

Hi I need a little help with the email-order-items.php

I added the purchase-note to each order position. This works fine with an entry in the function.php.
But if it is possible I wuuold like to change the lines in the email.
In start you can see the as is situation and in Ziel you can see the wished result.
How can I do this.
Thanks for a help.
Best
Start
Ziel

@jaffa-cake
Copy link

Hi, thank you for this! I'm trying to display the custom field text on the single product page instead of the email (through functions.php), but the code doesn't work there. It says, "Call to a member function get_id() on null" Could you possibly tell me how to adapt it? I would really appreciate it. :)

@heimvorteil
Copy link

Hi
I found an other solution with the plugin HTML Mail (https://codemiq.com/en/plugins/wp-html-mail-woocommerce/).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment