Added Function for getting all products(even disabled ones)
This commit is contained in:
9
get-all-single-products/README.md
Normal file
9
get-all-single-products/README.md
Normal file
@@ -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
|
||||
119
get-all-single-products/script.js
Normal file
119
get-all-single-products/script.js
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user