How do I update quantity and totals in a custom cart repeater automatically?

Question:
Hello everyone

I would, please, need help with a problem I am having.

I created my own cart(I don’t use the WIX one). Everything works (calculations, addition, deletion, updating of totals, etc.) except clicking on the “+” button or the “-” button: I would like that :
1/ the quantity, field No. 2 in the screenshot, is increased (or decreased) and modifies the field in the table.
2/ the total, field No. 3 in the screenshot, is calculated automatically and the data in the database is also modified)

Actually, i made a query based on the name of the customer and the name of article (in my “Panier” Database. i think it would be better to do this query with the ID? but how?

I am attaching my code as well as a screenshot of my cart.

THANKS A LOT FOR YOUR HELP …

Product:
WIX EDITOR

Hi everybody

I was able to make progress in partially resolving the problem.
From now on, it selects the right item (from its ._id) and it increments the quantity, and the total, as well as the Total cart.
My new problem: it only works once. If I click a second time on the “+” or “-” button “-”, nothing more happens. I
in fact, i noticed that if the starting quantity (for example is at 4), when i click on “+” button, it increases of course (5). But it’s still 4 in memory. (and modified to 5 in the database) so when i click a second time, it adds again 1 to 4 (always). It doesn’t take car of the new value (5), so he always adds from the starting value. How to refresh with the new value?

Thanks for your help

//****************************
//HERE THE CODE
//****************************

const AjoutQuantite = () =>{

return wixData.query('Monpanier')

.eq("_id",itemData._id)
.find()   
.then(async(results) => {	

	// Mise a jour du champ Quantite et total
	console.log(results.items[0]); 
	const MAJQteTotal = results.items.map(item => ({ ...item, quantite:   itemData.quantite+1,total:itemData.prix*(itemData.quantite+1),TotalFormate:String(parseFloat(Number(itemData.quantite+1)*itemData.prix).toFixed(2))+" €" }))
	//const MAJQteTotal = results.items.map(item => ({ ...item, quantite: itemData.quantite+1}))

	await wixData.bulkUpdate('Monpanier', MAJQteTotal);		

	$w('#Panier').setFilter(wixData.filter()
	.eq("mailClt", $w('#MailClt').value))
	.then(updateitem)
	.then(CalculTotalPanier)
	.then(result3)
	.then((results) => {
	console.log(results);
})
})

}

What kind of → TRIGGER/EVENT ← is starting your → AjoutQuantite() <— function ?

Hi CODE-NINJA
a button in my repeater called “ButtonPlus” :

$item(‘#ButtonPlus’).onClick(AjoutQuantite)

I can’T see any → REPEATER-CODE <—

Wait let me activate my 7th sense, ohhhhhh, yes now i can see something…

  1. Your REPEATER is maybe connected to a → DATASET ?
  2. Where do i find the code for feeding/updating your repeater? → something like
    a) $w(‘#myRepeater’).data = [here your data you want to feed your repeater with]
    b) But if you are using a dataset → REFRESH? maybe you forgot to refresh your dataset, sice you have changed your DATABASE, because your dataset is connected to your database???

Ohhhh, now i even found your → DATASET
$w(‘#Panier’).setFilter(wixData.filter() <— isn’t it a → DATASET ?

  1. What about → $w(‘#Panier’).refresh(); ???
  2. Your REPEATER is connected to a DATASET, right?
  3. What about —> $w(‘#Panier’).onReady() ← did you use it?

You will surely have more code, which you do not show, am i right ?

Always the same issues when mixing → WIX-DATA ← & → DATASETS <—

yes connected to a DATASET called “Mon panier”
so no code to feed may repeater.
The “refresh command” has been added : the pb persists.

yes I have other codes : the calculate the total in the cart
//4°/ CALCUL DU TOTAL SUR LA PAGE

const filtre = wixData.filter().eq(“mailClt”, SessionMailClt);

async function CalculTotalPanier(){
let calcul =await wixData.aggregate(“Monpanier”).sum(“total”,“TotalTTC”).filter(filtre).count().run();
let TotalTTC= calcul.items[0].TotalTTC
let QtePanier=calcul.items[0].count;

$w(‘#QtePanier’).value=QtePanier

//if (QtePanier> 0) {
if ((QtePanier) !== undefined) {
console.log (TotalTTC)
console.log (“Nombre d’articles : “+QtePanier+” Pour un Total Panier = “+TotalTTC+” €”)
$w(‘#TotalTTC2’).value=String(parseFloat(Number(TotalTTC).toFixed(2)))+" €"
$w(‘#TotalTTC’).value=String(Number(TotalTTC))

	let SessionMailClt
	SessionMailClt=session.getItem('MailClt');
	
	$w('#Panier').setFilter(wixData.filter()				
	.eq("mailClt", SessionMailClt))
	.then (Refresh)

} else {
	let SessionMailClt
	SessionMailClt=session.getItem('MailClt');

	console.log("Pas d'articles dans le panier pour "+SessionMailClt)
	$w('#TotalTTC2').value="0,00€"
	$w('#TotalTTC').value="0"

	$w('#Panier').setFilter(wixData.filter()				
	.eq("mailClt", SessionMailClt))
	.then (Refresh)
}

}

And finally, I created this function:

//4°/ Rafraichir les datasets

function Refresh() {
$w(‘#Panier’).refresh()
}

I’m sorry, but I’m a beginner. And totally self-taught, so I try …

Try like this

`$w(“#myRepeater”).onItemReady(($item, itemData, index) => {
$item(“#ButtonPlus”).onClick(()=>{
itemData.quantite=itemData.quantite+1;
itemData.total = itemData.prix*(itemData.quantite+1)
itemData.TotalFormate = String(parseFloat(Number(itemData.quantite+1)*itemData.prix).toFixed(2))+" €"
wixData
.update(“Monpanier”, itemData)
.then((results) => {
console.log(results); //see item below
})
.catch((err) => {
console.log(err);
});

})
});`

2 Likes

Hi rajmohanv2797

I would like to thank you for your help: the code works perfectly. I just had to refresh the dataset: everything is working: TOP.
thanks again

:grinning: :+1:

2 Likes