I finally got the solution!!!
I’ve now got a backend module called cancelCurrentMemberOrders.jsw
With this code:
import { orders } from 'wix-pricing-plans-backend';
export async function myListCurrentMemberOrdersFunction(filters, sorting, paging) {
try {
const listedOrders = await orders.listCurrentMemberOrders({ orderStatuses: ['ACTIVE', 'CANCELED'] }, { fieldName: '_createdDate', order: 'ASC' }, { limit: 3 });
for (const order of listedOrders) {
const orderId = order._id;
const orderStatus = order.status;
// Call myCancelOrderFunction for each order
await myCancelOrderFunction(orderId, 'NEXT_PAYMENT_DATE');
}
return listedOrders;
} catch (error) {
console.error('Error in myListCurrentMemberOrdersFunction:', error);
}
}
export async function myCancelOrderFunction(orderId, effectiveAt, options) {
try {
const order = await orders.cancelOrder(orderId, effectiveAt, {suppressAuth: true});
return order;
} catch (error) {
console.error(`Error in myCancelOrderFunction for order ${orderId}:`, error);
}
}
And this is the code in my frontend:
import { myListCurrentMemberOrdersFunction, myCancelOrderFunction } from 'backend/cancelCurrentMemberOrders';
export function button10_click(event) {
// Calling myListCurrentMemberOrdersFunction
myListCurrentMemberOrdersFunction()
.then(ordersList => {
// Iterating through each order and calling myCancelOrderFunction
return Promise.all(ordersList.map(order => {
const orderId = order._id;
const effectiveAt = 'NEXT_PAYMENT_DATE';
return myCancelOrderFunction(orderId, effectiveAt);
}));
})
.catch(error => {
// Handle errors if needed
});
}
$w.onReady(function () {
// Add an onClick event handler to your button
$w('#button10').onClick(button10_click);
});
Now, I don’t know if the code is super smooth. I spent a lot of time going through the API references, and got some help from … chatGPT
(alongside the help from @CODE-NINJA )
Anyways – I can now, FINALLY, let my members cancel their own orders, WITHOUT the restricted WIX MEMBERS AREA.
I’ve got a custom members database that works, and Wix Pricing Plans Work, and now with the above code, I have a button that cancels all orders of the currentMember.