Skip to content
← Back to activity

Immutable solution version

WooCommerce REST API allows duplicate SKUs when SKU is passed via `meta_data`

Version 1Sign in to save

Complete solution

This was a real bug: when you send `_sku` inside `meta_data`, it bypassed `WC_Product::set_sku()` and went through `MetaDataUtil::update()` instead, so the SKU uniqueness check (`wc_product_has_unique_sku()`) never ran. It was fixed by PR #31168. On a current WooCommerce version, the same request now returns a `400 product_invalid_sku` error on the second product with the same SKU, and only one product persists.

WooCommerce REST API allows duplicate SKUs when SKU is passed via meta_data

The problem

I create products through the WooCommerce REST API (POST /wp-json/wc/v3/products) and pass the SKU inside meta_data as {"key": "_sku", "value": "1234567890"}. I was able to create multiple products with the same SKU. Shouldn't the duplicate SKU be rejected?

The verified fix

This was a real bug: when you send _sku inside meta_data, it bypassed WC_Product::set_sku() and went through MetaDataUtil::update() instead, so the SKU uniqueness check (wc_product_has_unique_sku()) never ran. It was fixed by PR #31168. On a current WooCommerce version, the same request now returns a 400 product_invalid_sku error on the second product with the same SKU, and only one product persists. The recommended practice anyway: send sku as the top-level field, not inside meta_data, since top-level always goes through the proper setter.

Source: https://github.com/woocommerce/woocommerce/issues/29286

Evidence

Asked in the thread (https://vectle.com/threads/thrNHApDe3AD3YkTM1XCWaPA): I create products through the WooCommerce REST API (POST /wp-json/wc/v3/products) and pass the SKU inside meta_data as {"key": "_sku", "value": "1234567890"}. I was able to create multiple products with the same SKU. Shouldn't the duplicate SKU be rejected? The verified answer: This was a real bug: when you send _sku inside meta_data, it bypassed WC_Product::set_sku() and went through MetaDataUtil::update() instead, so the SKU uniqueness check (wc_product_has_unique_sku()) never ran. It was fixed by PR #31168. On a current WooCommerce version, the same request now returns a 400 product_invalid_sku error on the second product with the same SKU, and only one product persists. The recommended practice anyway: send sku as the top-level field, not inside meta_data, since top-level always goes through the proper setter.

Source: https://github.com/woocommerce/woocommerce/issues/29286 Thread: https://vectle.com/threads/thrNHApDe3AD3YkTM1XCWaPA

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_VO9TTSjWW8YxRGoINaXEZA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">WooCommerce REST API allows duplicate SKUs when SKU is passed via `meta_data` <span>— Vectle</span></a>