WooCommerce Displaying Stock Code in Shopping Cart

Stock code does not appear by default on the WooCommerce shopping cart page. You may want to show the stock code in the WooCommerce shopping cart so that you can communicate comfortably with your customers and your shipping / warehouse operation center can easily understand which product is being sold. The best and simplest way […]

WooCommerce Displaying Stock Code in Shopping Cart

Stock code does not appear by default on the WooCommerce shopping cart page. You may want to show the stock code in the WooCommerce shopping cart so that you can communicate comfortably with your customers and your shipping / warehouse operation center can easily understand which product is being sold.

The best and simplest way to do this is to add a simple code to the functions.php file in your theme. In the image below, you can see how the stock code looks like on the cart page.

If you want to show the stock code in the shopping cart in this way, you must first add the stock code to your products. Add an inventory code from product data> stock field to the products you add from WooCommerce> Products> Add New. Take a look at the image below for an example usage;

Repeat this process and add stock codes to all of your products. Please remember, stock codes also appear in Google searches and are among the factors that affect e-commerce SEO. Having a stock code on products in an e-commerce site allows the site to be in a more understandable hierarchy.

Add Shortcode to Your Theme

Add the code below to your theme’s functions.php file. This file is located in wp-content> themes> YOUR THEME> functions.php area.

add_action( 'woocommerce_after_cart_item_name', 'wpcomtr_sku_below_cart_item_name', 11, 2 );
 
function wpcomtr_sku_below_cart_item_name( $cart_item, $cart_item_key ) {
   $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
   $sku = $_product->get_sku();
   if ( ! $sku ) return;
   echo '<p><small>STOCK CODE: ' . $sku . '</small></p>';
}

If you do not know how to do this, you can use the Snippets plugin. Snippets plugin is a WordPress plugin that allows you to add custom code to your theme’s functions.php file. Take a look at the image below for how to add shortcode to the Snippets plugin;

Shortcode Not Working?

The code I shared above has been tested with the latest version of WooCommerce. Please let me know in the comments section about adding the code or if the code doesn’t work.

You can follow the WooCommerce category for this and similar WooCommerce help articles.

Leave a Reply

Your email address will not be published. Required fields are marked *