Hi there. I’m new to the forum, have no Javascript background, and very basic coding background…so if you respond please hold my hand tightly
So, I’m opening up a Wix store to sell my art and want to sell limited edition prints. I’m a little surprised to find that there is no option to display the available inventory, which seems like a pretty normal option, and in my estimation a very easy thing to implement given that inventory tracking is already built in. So I reluctantly forayed into the wix code to try to figure out a workaround. I’m also a little disappointed to find that there seems to be no access to the actual code running the site(like I could do on my shopify page), which might allow for minor tweaks like this (though I expect I would be overwhelmed with that).
Anyway, I came up with this workaround, which I was amazed that I actually got to work:
- Added text element to the product page (seen in red), named it #quantityRemainingText
Then here’s the code I came up with to access the product’s .quantityInStock property and assign the results to display in my text element.
let productQuantityNum = 200; //initialize product quantity variable
let soldOutText = "Edition Sold Out" //initialize variable to display "sold out" text when inventory is 0
$w.onReady(function () {
//TODO: write your page related code here...
$w('#productPage1').getProduct() //get current product
.then( (product) => {
productQuantityNum = product.quantityInStock; //get quantity in stock for current product
if (productQuantityNum > 0) { //if it's greater than 0, Display number remaining
let productQuantityString = productQuantityNum.toString();
$w("#quantityRemainingText").text = productQuantityString + " remaining";
} else {
$w("#quantityRemainingText").text = soldOutText; //display sold out text
}
} )
.catch( (error) => {
console.log(error);
} );
});
After some playing around, I got this to work, hallelujah! But it doesn’t do exactly what I want
The problem is that the product.quantityInStock property seems to hold the combined inventory for all the varients. So lets say I have two varients at 100 items each, my code displays “200 available”. What I really want is for my text to display the number available for the selected product option.
So, I looked and found there are a number of objects in the Wix api that relate to the product options (StoreProductOption, StoreProductOptionsChoice, etc. and the ProductOptionsAvailability), however, I don’t see any mention of there being a quantityInStock property for the specific product option as there is for the general product object.
So, can anyone tell me if there is such a property? If so, how can I retrieve it and implement it into my code?
Remember, please hold my hand. It may seem that I know what I am doing, but I’m only good at copying and pasting from example code and playing with it until it works So, any code snippets you could provide would be highly appreciated. Or if there are different or better ways of accomplishing my goal, please let me know.
Cheers!
Jonathan