How to Display SKU in the WooCommerce Cart – 2024 Guide

WooCommerce only allows you to display the SKU number on the product page but if you want to show it in the WooCommerce cart and the checkout pages, there is no such way. 

You’ll need to use the custom solution for this. 

In this article, I’ll share the custom piece of code that you can simply copy and paste to your store to show the SKU in the WooCommerce cart and the checkout. I’ll also explain the code so you can also have a better understanding of it. 

Without any further ado, let’s jump into the tutorial. 

How to Show SKU in WooCommerce Cart and Checkout?

The following is the code and the steps to show the product SKU number in the cart and checkout.

1. Copy the Code to Show SKU in WooCommerce Cart and Checkout

The following is the code to get the results you want. 

Code:

// Display the product SKU in the cart and checkout pages
function aovup_display_product_sku_in_cart_and_checkout($item_data, $cart_item) {
    // Get the product ID from the cart item
    $product_id = $cart_item['product_id'];

    // Get the SKU of the product
    $product = wc_get_product($product_id);
    $product_sku = $product->get_sku();

    // Add the SKU to the cart item data
    if (!empty($product_sku)) {
        $item_data[] = array(
            'key' => 'SKU',
            'value' => $product_sku,
        );
    }

    return $item_data;
}
add_filter('woocommerce_get_item_data', 'aovup_display_product_sku_in_cart_and_checkout', 10, 2);

2. Understand the Code (Optional)

The following is the step-by-step explanation of the above code:

  • The code defines a PHP function named aovup_display_product_sku_in_cart_and_checkout. This function takes two parameters: $item_data and $cart_item. These parameters are automatically passed by WooCommerce when the function is hooked into the woocommerce_get_item_data action.
  • Inside the function, it first extracts the product ID from the $cart_item array. The product ID is a unique identifier for the product added to the cart.
  • It then uses the wc_get_product() function to retrieve a WooCommerce product object based on the product ID. This object contains all the product information.
  • Next, it uses the get_sku() method on the product object to retrieve the SKU of the product. The SKU is a unique identifier for the product that is often used in inventory management.
  • If a product SKU is found and is not empty, it adds the SKU to the $item_data array. This array stores data about the cart item, and the SKU is added as an additional piece of information with a key of ‘SKU’ and the SKU value.
  • Finally, the function returns the updated $item_data array, including the SKU information.
  • The last line of code hooks the aovup_display_product_sku_in_cart_and_checkout function into the woocommerce_get_item_data action using the add_filter function. This action is fired by WooCommerce when item data is being retrieved for display in the cart and checkout pages. The 10 and 2 arguments indicate the priority and number of accepted arguments for the filter.

3. Paste the Code to WordPress

To paste the code to your WordPress and WooCommerce store, there are several methods and you can use any of them. Here’s the guide on adding code snippets to WordPress to help you with all the available methods. 

I have used a plugin to add the code to my demo store. 

4. Final Results

After adding the code to the store, let’s test it to see if it is working or not. 

To test it, I added a product to my cart and visited the cart page. I could see the SKU number below the product name on the cart page

After that, I proceeded to the checkout page and I could also see the SKU number below the product name. 

Note: I am using a custom checkout that I created with the help of AIO Checkout. You can create the same checkout by following the Shopify Checkout in the WooCommerce tutorial. 

Conclusion

That’s it for today’s article. 

I hope it was helpful to you and you were able to show the SKU in the WooCommerce cart and checkout pages without any difficulty. The code is also very short and simple to understand. It will help you display the SKU numbers on the cart and checkout page to reduce the errors in order and better inventory management.

Brian
Brian

Welcome to the AovUp blog, where we discuss all things WooCommerce. I hope we can help you achieve something today...

Leave a Reply

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

Unlock Your

25% Discount

Coupon Code: FESTIVE25

We – and our partners – use cookies to deliver our services and to show you ads. By using our website, you agree to the use of cookies as described in our Cookie Policy