I’m having trouble with
Briefly explain what’s not working or where you’re stuck
Working in
Wix Studio, CMS
Site link
https://www.nomfphy.com/items/“i’ll-take-the-ghosts”-necklace
What I’m trying to do
Tryna get the value of the credits field to populate the credits.
What I’ve tried so far
Looked up, logged the collection item details on terminal to check if there (it isn’t)
Extra context
Code:
import { currentCart } from "wix-ecom-backend";
import { product } from "wix-stores-frontend";
import wixEcomFrontend from "wix-ecom-frontend";
import wixLocationFrontend from 'wix-location-frontend';
const WIX_STORES_APP_ID = "215238eb-22a5-4c36-9e7b-e7c08025e04e";
$w.onReady(function () {
const choices = {};
let thisProduct;
$w("#itemsDataset").onReady(() => {
thisProduct = $w("#itemsDataset").getCurrentItem();
console.log(thisProduct);
populateCredits();
populateOptions();
$w('#add').text = "ADD (" + thisProduct.reference.formattedPrice + ")";
})
async function populateCredits() {
$w('#credits').onItemReady(($item, itemData) => {
console.log($item, itemData);
$item('#cText').text = itemData.title;
$item('#cImage').src = itemData.image;
$w('#credit').onClick(async () => {
wixLocationFrontend.to(itemData.link);
})
})
$w('#credits').data = thisProduct.credits;
if (thisProduct.credits.length > 0) {
$w('#credits').collapse();
}
}
const populateOptions = () => {
const optionsData = [];
const optionKeys = Object.keys(thisProduct.reference.productOptions);
let changed = false;
optionKeys.forEach((key, index) => {
const option = thisProduct.reference.productOptions[key];
if (option.choices.length !== 1) {
optionsData.push({ ...option, _id: index.toString() });
if (index + 1 === optionKeys.length) {
$w("#options").data = optionsData;
changed = true;
}
}
});
if (!changed) {
$w("#options").data = [];
}
}
$w('#quantity').onBlur((event) => {
if (event.target.value.includes("X")) {
if (Number.isInteger(Number(event.target.value.slice(1)))) {
event.target.value = "X" + Math.min(Math.max(Number(event.target.value.slice(1)), 1), 99)
} else {
event.target.value = (Number.isInteger(Number(event.target.value))) ? "X" + Math.min(Math.max(Number(event.target.value), 1), 99) : "X1";
}
} else {
event.target.value = (Number.isInteger(Number(event.target.value))) ? "X" + Math.min(Math.max(Number(event.target.value), 1), 99) : "X1";
}
$w('#add').text = "ADD (" + thisProduct.reference.price * Number(event.target.value.slice(1)) + ")";
})
$w("#options").onItemReady(($item, itemData) => {
$item("#oDropdown").options = itemData.choices.map((choice) => ({ "label": choice.description, "value": choice.description }));
$item("#oDropdown").onChange(async () => {
choices[itemData.name] = $item("#oDropdown").value;
const variantPrice = await getSelectedVariantPrice();
$w('#add').text = "ADD (" + variantPrice + ")";
})
})
async function getSelectedVariant() {
const variants = await product.getVariants(thisProduct.reference._id, { "choices": choices });
return variants[0]._id;
}
async function getSelectedVariantPrice() {
const variants = await product.getVariants(thisProduct.reference._id, { "choices": choices });
return variants[0].variant.formattedPrice;
}
async function addToCart() {
const itemOptions = {
variantId: undefined,
options: undefined,
};
const options = {
lineItems: [{
catalogReference: {
appId: WIX_STORES_APP_ID,
catalogItemId: thisProduct.reference._id,
options: itemOptions
},
quantity: Number($w('#quantity').value.slice(1)),
}, ],
};
if (thisProduct.reference.manageVariants) {
itemOptions.variantId = await getSelectedVariant();
} else {
itemOptions.options = await choices;
}
await currentCart.addToCurrentCart(options);
wixEcomFrontend.refreshCart();
wixLocationFrontend.to('/shop');
}
$w('#add').onClick(() => {
addToCart();
})
});
Item data output:
