Allow Duplicate SKU

By default it is not allowed to use duplicate SKU in WooCommerce, you might get an error like this if you try to save a product with a SKU which already exists:

Invalid or duplicate SKU error – how to allow duplicate SKU in WooCommerce

But there are cases where duplicate SKU is necessary! So what are the options?

You can allow duplicate SKU by adding this single line of code into your WooCommerce store:

add_filter( 'wc_product_has_unique_sku', '__return_false', 99 );

Please read this in case you don’t know how to use code snippets to your website.

Ok, but the main question here – is there any particular reason why you can’t allow duplicate SKU in WooCommerce by default? Is allowing it going to break something?

Let’s figure it out!

Things which are going to Stop Working Correctly on our WooCommerce Store after we Allow Duplicate SKU

Bad news – some WooCommerce functionality which relies on having unique SKU on your store is going to stop working correctly. Good news – maybe you’re not even going to notice it, because you’re not using these features, perhaps.

[add_to_cart], [add_to_cart_url] and [product_page] shortcodes

Shortcodes [add_to_cart], [add_to_cart_url] and [product_page] accept sku parameter which allows to display either an “Add to cart” button or a URL or page of a particular product with a specific SKU.

But since we don’t have unique SKUs anymore, these shortcodes are just going to get a first found product which has a provided SKU (usually the one with the lowest ID).

What we can do in this case?

Instead of [add_to_cart sku="sku123"] just use [add_to_cart id="123"]. Easy so far.

Adding line items to an order using WooCommerce REST API

Comparing to the shortcodes it could be a more serious issue for you, but only in case you’re using WooCommerce REST API (or any plugins which repy on it).

Let’s assume that you’re creating WooCommerce orders using the REST API and you’re adding products to orders by their SKU:

$order_data = array(
	'payment_method' => 'bacs',
	'payment_method_title' => 'Direct Bank Transfer',
	'set_paid' => true,
	'billing' => array(
		'first_name' => 'Misha',
		'last_name' => 'Rudrastyh',
		'city' => 'Colombo',
		'country' => 'LK',
		'email' => 'no-reply@rudrastyh.com',
	),
	'line_items' => array(
		array(
			'sku' => 'sku123',
			'quantity' => 2,
		),
	),
);

$woo_api->post( 'orders', $order_data );

By the way, if this code is unfamiliar to you – it is just how you can interact with the REST API using official PHP library.

Ok, but how to fix?

Instead of a SKU we will need to provide a product ID in our requests, which in some cases could be quite problematic for sure:

'line_items' => array(
	array(
		'product_id' => 123,
		'quantity' => 2,
	),
),

CSV Product Import

If you’re going to run a product import tool, products which have the same SKU in your CSV import file are going to be ignored.

How to fix?

Obviously, you need to exclude the SKU column from the import:

Exclude duplicate SKU from WooCommerce product import

Product image matching by SKU

If you go to WooCommerce > Settings > Products > Advanced, you will find a very interesting checkbox there:

Product image matching by SKU settings page
This checkbox is unchecked by default.

Basically how it works is when you upload an image woo-tshirt.png and you have a product on your store with woo-tshirt SKU, then this uploaded image will automatically become a product image.

If you have multiple products with the same SKU, then the feature image will only be updated for the first found product with this specific SKU (with the smallest ID).

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X