Hello there! Hopefully this is the correct category to post under. First time posting in the forum!
This issue has racked my brain all day. I really just think it comes from not understanding how to use async / await. I’m trying to query the cart product IDs so that I can find their collection IDs. From there, I am trying to add items from a certain collection to a counter, and, if less than 2, redirect to a minimum order notification.
The code seems to work until I enable the line of code: currentCart.lineItems.forEach(async (lineItem) => {
When I enable this, the counter at the bottom shows null. Can anyone help with this? I am so lost when it comes these type of promises. >.>
// API Reference: https://www.wix.com/velo/reference/api-overview/introduction
// “Hello, World!” Example: https://learn-code.wix.com/en/article/1-hello-world
import wixData from 'wix-data';
import { cart } from 'wix-stores';
let finalProduct = [];
let products = [];
let i = 0;
let sweatshirtCounter = [];
$w.onReady(async function () {
const currentCart = await cart.getCurrentCart()
// if (await lineItems.length < 3) {
// console.log("Cart items less than 3. Searching to see if any products are sweatshirts.")
//////////////this is where my problem seems to be///////////////
//currentCart.lineItems.forEach(async (lineItem) => {
///////////////////////////////
let productCollections = [];
const results = await wixData.query("Stores/Products")
.eq("_id", "6616171d-13d7-288b-4a6b-a9542625a442")
////////////^ to be replaced with lineItem.productId//// for now using 1 id for testing
.include("collections")
.find()
if (results.items.length > 0) {
let item = results.items[0]; //see item below
console.log("item:", item)
item.collections.forEach((collectionItem) => {
if (collectionItem.name === "Sweatshirt") {
sweatshirtCounter.push({ "count": 1 })
}
})
} else {
console.log("No product found.")
}
console.log("counter:", sweatshirtCounter)
if (sweatshirtCounter.length < 2) {
console.log("Sweatshirts less than 2")
}
})