I’m trying to make a simple app to update prices in WooCommerce through the REST API.
I have arrayOne which has SKU + updated prices from my ERP
[
{sku=PD-1000-B, price=9800.00},
{sku=PD-1007-A, price=9678.16}
]
arrayTwo which has all products from woocommerce, SKU, id (which I need to get) and current price
[
{sku=PD-1000-B, id=1622.0, price=8145.9},
{sku=PD-1007-A, id=1624.0, price=9678.16}
]
I would like to compare arrayTwo with arrayOne, if price in arrayOne is bigger then with all coincidences combine and create new array. Something like this
[
{sku=PD-1000-B, id=1622.0, price=9800.00}
]
Since the only bigger price is for PD-1000-B.
All I got so far is to create a new Set but that doesn’t compare anything, the idea would be to reduce only to changes the amount to update.
EDIT: As of right now I’m merely replacing the price in arrayTwo with the one in arrayOne using this:
let set = new Set();
for (let i = 0; i < data.length; i++)
set.add(data[i]['sku'])
for (let i = 0; i < container.length; i++)
if (set.has(container[i]['sku']))
container[i]['price'] = data[i]['price'];
container is an empty variable I’m using to collect the data I get from WooCommerce as an array and data is a the array which contains the updated prices + sku.
Thank you.
2
Answers
You can achieve this in two steps:
arrOne
arrTwo
You can use this nice function for that:
I’d try it this way: