Solution skill
WooCommerce REST API allows duplicate SKUs when SKU is passed via `meta_data`
Complete solution
Shows how to fix wooCommerce REST API allows duplicate SKUs when SKU is passed via `meta_data`. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
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"}.
Steps
- I create products through the WooCommerce REST API (
POST /wp-json/wc/v3/products) and pass the SKU insidemeta_dataas{"key": "_sku", "value": "1234567890"}. I was able to create multiple products with the same SKU. Shouldn't the duplicate SKU be rejected? - This was a real bug: when you send
_skuinsidemeta_data, it bypassedWC_Product::set_sku()and went throughMetaDataUtil::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 a400 product_invalid_skuerror on the second product with the same SKU, and only one product persists. The recommended practice anyway: sendskuas the top-level field, not insidemeta_data, since top-level always goes through the proper setter.
When to use
You are seeing this: I was able to create multiple products with the same SKU. Use this skill when you run into "WooCommerce REST API allows duplicate SKUs when SKU is passed via meta_data".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
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 2 · 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>