-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerGroupsApp.js
59 lines (51 loc) · 2.26 KB
/
CustomerGroupsApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { get, put } from 'axios';
const storeId = 98501509;
const publicToken = 'Bearer public_PUb7yJJDWc5RYj1nL7aXHJFT5j5bn2hW';
const groupID = '23865254';
const products = ['625285756','625238809', '625270554', '625285757'];
const categoryIDs = ['163181566','163184815','163191832'];
// Function to apply discounts for a specific customer group
async function applyDiscounts() {
try {
// Iterate over category IDs
for (const categoryId of categoryIDs) {
// Fetch products from Ecwid based on category
const productsResponse = await get(`https://app.ecwid.com/api/v3/${storeId}/products`, {
params: {
token: publicToken,
categoryId: categoryId // Include category ID in the request
}
});
const products = productsResponse.data.items;
// Iterate over products
for (const product of products) {
// Check if the product is in the list of products to apply discount to
if (!products.includes(product.id)) {
continue;
}
if(!product.wholesalePrice){
console.log(`No wholesale pricing avialable for Product ID ${product.id}`);
continue;
}
// Calculate discounted price (wholesale cost)
const discountedPrice = parseFloat(product.wholesalePrice) * 1.20;
// Update product price in Ecwid
await put(`https://app.ecwid.com/api/v3/${storeId}/products/${product.id}`, {
price: discountedPrice //Updates the price
}, {
params: {
token: publicToken
}
});
console.log(`Discount applied for product ID ${product.id}. New price: ${discountedPrice}`);
}
console.log(`Discounts applied successfully for products in category ID ${categoryId}.`);
}
} catch (error) {
console.error('Error applying discounts:', error.response ? error.response.data : error.message);
}
}
// Call function to apply discounts when page loads
window.onload = function() {
applyDiscounts();
};