Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate products with identical SKUs created via API #27295

Open
3 tasks done
rafal-sokolowski opened this issue Sep 18, 2019 · 29 comments · May be fixed by #47476
Open
3 tasks done

Duplicate products with identical SKUs created via API #27295

rafal-sokolowski opened this issue Sep 18, 2019 · 29 comments · May be fixed by #47476
Assignees
Labels
focus: product Issues related to product or product page. focus: rest api Issues related to WooCommerce REST API. impact: high To be added by the PR author/reviewer if the PR could very likely cause SEV-1 or SEV-2 issues plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. team: Proton type: bug The issue is a confirmed bug.

Comments

@rafal-sokolowski
Copy link

Description of the problem
The syncing system sends requests to /wp-json/wc/v3/products/batch endpoint. It usually creates concurrent connections to the WooCommerce API. Sometimes, it happens that if two or more requests contain information about the same product and they run at the same time we end up with multiple products created having identical SKUs.

Steps to reproduce

curl -X POST \
  https://shop.test/wp-json/wc/v3/products/batch \
  -H 'Authorization: Basic Y2tfZWExNmNmNzVkZjkyMWM4YWZmYmI2MjAxYmZiMjU0ODAwNjNhYzk2MTpjc183MDAzOGMxNTRiZWVlYmEyMTU2ZmQ4MGQ2NWE5ZmM1NzcyMGY5M2Mz' \
  -H 'Content-Type: application/json' \
  -d '{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345
        }
    ]
}' -k & curl -X POST \
  https://shop.test/wp-json/wc/v3/products/batch \
  -H 'Authorization: Basic Y2tfZWExNmNmNzVkZjkyMWM4YWZmYmI2MjAxYmZiMjU0ODAwNjNhYzk2MTpjc183MDAzOGMxNTRiZWVlYmEyMTU2ZmQ4MGQ2NWE5ZmM1NzcyMGY5M2Mz' \
  -H 'Content-Type: application/json' \
  -d '{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345
        }
    ]
}' -k

Expected behavior
By default, it should not be possible to add a new product with an existing SKU.

Isolating the problem (mark completed items with an [x]):

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.

WordPress Environment

``` Serverinfo: Apache/2.4.41 (Unix) OpenSSL/1.0.2s PHP/7.2.22 PHP version: 7.2.22 MySQL version: 5.5.5-10.4.6-MariaDB WooCommerce version: 3.6.4 WordPress version: 5.2.3 ```
@claudiosanches claudiosanches transferred this issue from woocommerce/woocommerce Oct 1, 2019
@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label type: bug to this issue, with a confidence of 0.98. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@juliaamosova
Copy link
Contributor

Hi @rafal-sokolowski,

Thank you for raising the issue!

However, I can't reproduce it on my end.

I make the following POST request to wp-json/wc/v3/products/batch endpoint with identical SKUs:

{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345678
        },
        {
            "name": "Unique SKU product 2",
            "type": "simple",
            "sku": 12345678
        }
    ]
}

As a result, I only get 1 product created as expected:

sku1

And here is to show that if SKUs are indeed unique in 2 products, both products get created as expected as well:

sku2

Are you still able to reproduce the issue you are having?

@juliaamosova
Copy link
Contributor

Hi @rafal-sokolowski,

As a part of this repository’s maintenance, I am closing this issue due to inactivity. Please feel free to comment on it in case we missed something. We’d be happy to take another look.

@rafal-sokolowski
Copy link
Author

Hi @juliaamosova,

Your test in Postman is wrong. It creates multiple products using one request. This works fine.

I specifically wrote that you can create multiple products with identical SKUs by making multiple API requests at the same time.

In order to reproduce that issue, you need to use curl as in my Steps to reproduce example above.

@rafal-sokolowski
Copy link
Author

curl -X POST \
  https://playground.test/wp-json/wc/v3/products/batch \
  -u ck_a55be1d63c5eb67e1aa8f206664c396c4b87a827:cs_433b9287757cbd328f18047e86baa92d23d67e7e \
  -H 'Content-Type: application/json' \
  -d '{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345
        }
    ]
}' -k & curl -X POST \
  https://playground.test/wp-json/wc/v3/products/batch \
  -u ck_a55be1d63c5eb67e1aa8f206664c396c4b87a827:cs_433b9287757cbd328f18047e86baa92d23d67e7e \
  -H 'Content-Type: application/json' \
  -d '{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345
        }
    ]
}' -k

image

@juliaamosova
Copy link
Contributor

Thank you for your follow-up @rafal-sokolowski. I am reopening this issue to take a look at it again in a bit.

@juliaamosova juliaamosova reopened this Jan 16, 2020
@stefanos82
Copy link

@juliaamosova @rafal-sokolowski I have been dealing with this issue for months now and it drives me crazy.

I first found this in October when a customer called about having thousands of duplicated and even quadruplicated products in draft and thought it was my script.

The missing two SKUs are my mistake; I have tried to delete them via PHPMyAdmin with some testing and instead of deleting the rows, I deleted the SKUs

image

I have designed my own mechanism that takes my customer's CRM data and sends them as batches (create / update) of 5 to their website via synchronization either every 20 minutes or immediate execution depending on customers' needs.

The problem takes place when they leave it running for hours.

Do you think scheduled transients play some role in all this nightmare, that is, does it intervene for some reason and interrupts the whole process?

@stefanos82
Copy link

stefanos82 commented Feb 26, 2020

@juliaamosova @rafal-sokolowski this is to verify that I have managed to reproduce your bug by following your aforementioned steps:

image

WordPress Environment

WordPress address (URL): https://local.demo
Site address (URL): https://local.demo
WC Version: 3.9.2
REST API Version: ✔ 1.0.7
Log Directory Writable: ✔
WP Version: 5.3.2
WP Multisite: –
WP Memory Limit: 256 MB
WP Debug Mode: –
WP Cron: ✔
Language: en_US
External object cache: –

Server Environment

Server Info: nginx/1.16.1
PHP Version: 7.3.15-3
PHP Post Max Size: 64 MB
PHP Time Limit: 3600
PHP Max Input Vars: 3000
cURL Version: 7.67.0
OpenSSL/1.1.1d

@vedanshujain vedanshujain transferred this issue from woocommerce/woocommerce-rest-api Aug 11, 2020
@vedanshujain
Copy link
Contributor

This looks like race condition because SKU uniqueness is probably not enforced by the database, just by the code. We need probably better protections around it.

@vedanshujain vedanshujain added type: bug The issue is a confirmed bug. focus: rest api Issues related to WooCommerce REST API. labels Aug 11, 2020
@sohail-khalil
Copy link

any update on this? I'm facing the same issue.

@ThomasVeld
Copy link

I'm running multiple Redis queues and it bypasses the SKU validation occasionally.
Any update is highly appreciated.

@philiporosz
Copy link

Any Update? Still facing this issue. @vedanshujain

@imaadt
Copy link

imaadt commented Dec 21, 2021

Is there an update to this bug? Still have this issue @vedanshujain

@vedanshujain
Copy link
Contributor

No update yet, unfortunately. This will take a while to resolve, unfortunately.

@harslannet
Copy link

Using C#, we sync products from a different platform to WooCommere with API. We had this problem too, when we saw that there was no solution, we saved the day with an alternative method.

  • First I put the SKUs in the API response from the other platform into a comma-separated string.
  • In the second step, I sent these SKUs to the /wp-json/wc/v3/products/?sku= endpoint (with parameter "_fields", "sku") and converted the returned response into a json object.
  • Finally I prepared a new json by excluding what is already in WooCommerce from the first api response.

(my english is not very well and I'm sorry about that.)

@ObliviousHarmony ObliviousHarmony added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Feb 21, 2022
@yobotn
Copy link

yobotn commented Feb 22, 2022

I have the same problem with wordpress 5.9 and woocommerce 6.2.0.

maybe if we can add the sku as a unique field in the mysql database can resolve the problem?

@JosephWJMaxwell
Copy link

Having same issue. When uploading 15000 products via api, it seems to be duplicating some of them.

@imaadt
Copy link

imaadt commented Apr 20, 2022

Is there an update on this bug yet? @vedanshujain

@peterfabian peterfabian added the priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. label May 5, 2022
@yobotn
Copy link

yobotn commented May 24, 2022

I believe the problem is the images size uploaded on the process on creating the product,
when i compressed the photos the problem went away.

@CostaMateus
Copy link

Any updates?

@PMLS3
Copy link

PMLS3 commented Aug 18, 2022

Anyone found a solution yet?

@yobotn
Copy link

yobotn commented Aug 18, 2022

When I compressed the product images the problem went away,
another method I have used in the beginning is to use webhooks to check every product upload and if duplicate delete it.

@PMLS3
Copy link

PMLS3 commented Aug 18, 2022

Thank you...

@rrennick rrennick added focus: product Issues related to product or product page. team: Proton labels Oct 31, 2023
@richardaubin
Copy link

On the woocommerce side of things, the only solution would be to create a lock on the process to prevent the race condition, and queue up any additional incoming requests.

On the application side of things, managing concurrency issues or providing your own sku creation algorithm would be prevent this error from happening. This is what I do in my .net/c# application.

@richardaubin
Copy link

richardaubin commented Mar 1, 2024

curl -X POST \
  https://playground.test/wp-json/wc/v3/products/batch \
  -u ck_a55be1d63c5eb67e1aa8f206664c396c4b87a827:cs_433b9287757cbd328f18047e86baa92d23d67e7e \
  -H 'Content-Type: application/json' \
  -d '{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345
        }
    ]
}' -k & curl -X POST \
  https://playground.test/wp-json/wc/v3/products/batch \
  -u ck_a55be1d63c5eb67e1aa8f206664c396c4b87a827:cs_433b9287757cbd328f18047e86baa92d23d67e7e \
  -H 'Content-Type: application/json' \
  -d '{
    "create": [
        {
            "name": "Unique SKU product",
            "type": "simple",
            "sku": 12345
        }
    ]
}' -k

What I don't understand about your situation is why you are duplicating the request. Can you not prevent this from happening in your code? Both of your requests claim to be unique sku products, however, it's obvious that is not the case. Prevent the duplicate request by ensuring your list of requests are truly unique. Problem solved.

@rafal-sokolowski
Copy link
Author

@richardaubin For the same reason you have a lock on your door. You can ask for no one to break in, but that's unlikely to work.

I agree that if you are the one controlling the data being pushed to the WooCommerce API, you can easily fix the problem.

However, the scenario I described is when it's a third-party solution that generates those requests, and you can't do anything about it except for asking other developers to fix it, which may never happen. Problem unsolved.

@fredrik-tveraaen-apiera
Copy link

Any updates?

@naman03malhotra naman03malhotra added the impact: high To be added by the PR author/reviewer if the PR could very likely cause SEV-1 or SEV-2 issues label May 22, 2024
@naman03malhotra
Copy link
Contributor

naman03malhotra commented May 28, 2024

Hey folks! we have an update regarding this issue. We have this PR #47476, which is proposed to fix this.
Can you please test if this fixes the issue on your end? You can test the branch sku-check-before-product-creation
OR with the zip
woocommerce.zip

NOTE: This fix only works for fresh product creation not for updates, so its best you try it on a fresh site or with a new set of products and skus.

Let us know if you face any issues or fatals.

@naman03malhotra
Copy link
Contributor

Please test it with this zip, updated a few things: woocommerce.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: product Issues related to product or product page. focus: rest api Issues related to WooCommerce REST API. impact: high To be added by the PR author/reviewer if the PR could very likely cause SEV-1 or SEV-2 issues plugin: woocommerce Issues related to the WooCommerce Core plugin. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. team: Proton type: bug The issue is a confirmed bug.
Projects
Status: Core bugs
Status: No status