diff --git a/get-all-single-products/README.md b/get-all-single-products/README.md new file mode 100644 index 0000000..4e51b8e --- /dev/null +++ b/get-all-single-products/README.md @@ -0,0 +1,9 @@ +# Get all Products # +Function that get array of product id's as an argument and uses that to get their data. It uses graphql "product" method to get every product individually to make sure it gets all products, even those with no product in warehouse. + +## Usage ## +``` +const products = await idmGetAllSingleProd([123,234,345,456]) +``` + +Created by • **[IdoMods](https://idomods.pl/)** • 2026 \ No newline at end of file diff --git a/get-all-single-products/script.js b/get-all-single-products/script.js new file mode 100644 index 0000000..28a2019 --- /dev/null +++ b/get-all-single-products/script.js @@ -0,0 +1,119 @@ +function idmGetSingleProdGraphQL(prodId) { + const IDM_PRICE_QUERY = (priceType) => `price { + rebateCodeActive + price { + ${priceType} { + value + formatted + } + } + omnibusPrice { + ${priceType} { + value + formatted + } + } + omnibusPriceDetails { + unit { + ${priceType} { + value + formatted + } + } + youSavePercent + omnibusPriceIsHigherThanSellingPrice + newPriceEffectiveUntil { + formatted + } + } + max { + ${priceType} { + value + formatted + } + } + unit { + ${priceType} { + value + formatted + } + } + unitConvertedPrice { + ${priceType} { + value + formatted + } + } + youSavePercent + beforeRebate { + ${priceType} { + value + formatted + } + } + beforeRebateDetails { + youSavePercent + unit { + ${priceType} { + value + formatted + } + } + } + advancePrice { + ${priceType} { + value + formatted + } + } + suggested { + ${priceType} { + value + formatted + } + } + rebateNumber { + number + ${priceType} { + value + formatted + } + } + }`; + + return ` + prod${prodId}: product(productId: ${prodId}) { + product { + id + name + link + ${IDM_PRICE_QUERY(app_shop.vars.priceType)} + } + }`; +} + +async function idmGetAllSingleProd(prodArr) { + try { + const res = await fetch("/graphql/v1/", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + query: `{${prodArr.reduce( + (acc, val) => acc + `${idmGetSingleProdGraphQL(val)}`, + "" + )}}`, + }), + }); + + const data = await res.json(); + const products = Object.values(data?.data)?.map((prod) => prod.product); + if (!products) + throw new Error("Coś poszło nie tak! Nie znaleziono produktow"); + + return products; + } catch (err) { + console.error(err); + } +}