Hi,
Can anyone help me to create a repeater from the data in the shopping cart?
I found the way to crate an array of the shopping cart using getCurrentCart()
but how can I show the data in a repeater?
Hi,
Have you tried using the onItemReady() function and the repeater.data API to populate the items in your repeater from the data in the array you retrieved?
Let us know what you have tried so far along with a snippet of your code so the community can help you out!
Best regards,
Miguel
Thanks,
I’m pretty sure I’m not in the right direction
here is my code
import wixStores from 'wix-stores';
import {getCurrentCart} from 'backend/getCurrentCart.jsw';
import wixData from 'wix-data';
$w.onReady(async function () {
const results = await getCurrentCart();
let cartProducts = results.lineItems;
let cartID = results._id;
$w("#repeater1").onItemReady( ($item, itemData, index) => {
$item("#pName").text = itemData.lineItems.name;
$item("#pImage").src = itemData.mediaItem.src;
} );
$w('#repeater1').data = results;
});
In the front end I solved it like this. Because the repeater wants an _id field I had to push items into a new array, and feed it to the repeater.data
import { getCurrentCart } from 'backend/store.jsw'
$w.onReady(async () => {
const cart = await getCurrentCart()
const repeater = $w('#repeater1')
const items = []
console.log(cart)
cart.lineItems.map(item => {
items.push({
_id: item.productId,
item: item
})
})
// Create rows in the repeater
repeater.onItemReady(($item, itemData) => {
row($item, itemData.item)
})
repeater.data = items
})
function row($item, {name, mediaItem}) {
$item('#pName').text = name
$item('#pImage').src = mediaItem.src
}
This is the backend:
import wixStoresBackend from 'wix-stores-backend'
export function getCurrentCart() {
return wixStoresBackend.getCurrentCart()
}
work’s amazing! thank you