Calculating discounted price as a percent not working

Hi Anthony,

thanks for your response. May I ask for a solution for following requirement:
I want to show in a Text Element the calculated amount of a discount in ‘%’.

Edit
I tried this:

$w.onReady(async function () {
    $w('#topItems').onItemReady( ($item, itemData) => {
        $item('#discountValue').text = (1-(itemData.discountedPrice / itemData.Price)*100).toString();
    } );

	const {items: collectionData} = await wixData.query('Stores/Products').find();
	console.log(collectionData);

    $w('#topItems').data = collectionData;
});

But it doesn’t work.

Am I right if I access the ‘Stores/Products’ collection to get the data?

To compute the discount in ‘%‘ as I would use the formula:

1–(discountedPrice/price))*100 that gives me the percentage of the relevant discount value.

But I’m not sure how to get that in a JS code?

Thanks!

Are you getting an error message in your web browser’s console? I noticed that your formula says itemData.Price should it be itemData.price?

Thanks! I found a solution meanwhile with:

$w.onReady(async function () {
    try {
        const {items: collectionData} = await wixData.query('Stores/Products').find();
        console.log(collectionData);

        if (collectionData && collectionData.length > 0) {
            $w('#containerName').data = collectionData;
            $w('#containerName').onItemReady(($item, itemData) => {
                if (itemData.discountedPrice && itemData.price) {
                    const discountValue = Math.round(1 - (itemData.discountedPrice / itemData.price) * 100);
                    $item('#textElementName').text = discountValue.toString();
                }
            });
        } else {
            console.log('No data found.');
        }
    } catch (error) {
        console.error('Error when retrieving the data:', error);
    }
});
1 Like

Awesome, glad you got that sorted out!