WooCommerce: Add SKU to checkout/order-received page

On the ‘checkout/order-received’ page it displays the name of the product and a total, but I need to add the SKU of the product(s).

The ‘order-details.php’ page:


<tbody>
		<?php
			foreach( $order->get_items() as $item_id => $item ) {
				$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );

				wc_get_template( 'order/order-details-item.php', array(
					'order'			     => $order,
					'item_id'		     => $item_id,
					'item'			     => $item,
					'show_sku'			 => $sku,
					'show_purchase_note' => $show_purchase_note,
					'purchase_note'	     => $product ? get_post_meta( $product->id, '_purchase_note', true ) : '',
					'product'	         => $product,
				
					
				) );
			}
		?>
		<?php do_action( 'woocommerce_order_items_table', $order ); ?>
	</tbody>

The ‘order/order-details-item.php’:

	<td class="product-name">
		<?php
			$is_visible        = $product && $product->is_visible();
			$product_permalink = apply_filters( 'woocommerce_order_item_permalink', $is_visible ? $product->get_permalink( $item ) : '', $item, $order );
			$sku = $product->get_sku();

			echo apply_filters( 'woocommerce_order_item_name', $product_permalink ? sprintf( '<a href="%s">%s</a>', $product_permalink, $item['name'] ) : $item['name'], $item, $is_visible );
			echo apply_filters( 'woocommerce_order_item_quantity_html', ' <strong class="product-quantity">' . sprintf( '&times; %s', $item['qty'] ) . '</strong>', $item );
			

			do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );

			$order->display_item_meta( $item );
			$order->display_item_downloads( $item );

			do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
		?>
	</td>

But I’m not getting the SKU to post.

Add this to the end of your themes functions.php file.

/**
 * Adds product SKU to the WooCommerce order details page
 * Uses WooCommerce 2.5 or newer
 */
add_action( 'woocommerce_add_order_item_meta', 'jk_add_item_sku', 10, 3 );
function jk_add_item_sku( $item_id, $values, $cart_item_key ) {

  $item_sku = get_post_meta( $values[ 'product_id' ], '_sku', true );

  wc_add_order_item_meta( $item_id, 'sku', $item_sku , false );

}

Note: this won’t do anything to existing orders, it will only work on orders that are made after the script is on the site.

Hope this helps!

1 Like

Thank you, Joelkuiper. That worked out excellent!

May I ask where you acquired this?

1 Like

3 years of working with WooCommerce :slight_smile:

Glad it worked for you!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.