Background: Members with a Wix Pricing Plan can create listings. These listings are stored in a CMS collection & displayed via dynamic pages. The collection has a Boolean field, used to manually hide a listing
Goal: When their plan ends, their listing should be hidden automatically
Suggestions of approaches (as a rookie coder)
- When plan ends, user’s corresponding collection item auto modifying itself
1.1) Using Wix Automations: Can you modify a CMS item directly with no code? Seems no option yet. Or is there?:
1.2) Wix Automations with code?
1.3) JavaScript object field?
Working in Wix Editor
Additional: Similar tutorial: Tutorial: Using the Pricing Plans API for Pricing Plan Ordering and Payment
AI attempts:
There are two ways to do this depending on the volume of users and the number of items that you have in your CMS:
#1 - Using event hooks
Similar to the approach shown by the AI, you can triggert a bulk update to toggle off (hide) one or more listings belonging to a particular user when their pricing plan subsciption ends. And restart it if they sign up for another subscription.
OR
#2 - Fetching data on the fly
In this you don’t need to have a toggle on / off column in your CMS, your code will directly fetch all active subscriptions and will automatically filter your entire list to show only those listings that belong to a user with an active subsription.
This latter method is what I have used in one of my client’s site, which is pretty content heavy and required this exact same functionaity. And the second option works like a charm. In my personal opinion, it is much more reliable, accurate and consistent as compared to the data hook method, since it runs the code and fetches live, real-time member subscription data every time the page loads.
No, there is no such option as of now to directly modify CMS items or control their visibility simply through automations, without using any code.
Thank you, you clarified all of them well! To implement #2 (or #1), is there a guide you can direct me to or code that I can try?
I’m not aware of a guide or a tutorial that demonstrates this use case, but I can explain the underlying logic flow for method #2, which you can use as a foundation to write your code.
- When the page loads, fetch all your active subscriptions using using the managementListOrders() function in the backend.
- Fetch the
memberId
associated with each subscription and form an array that you will return to the frontend.
- Based on how you’ve mapped the listings with your users, you can either use this array and directly pass it as a filter for the
_owner
field in your dataset, or fetch the login email for these subscribers and then filter your dataset accordingly.
This should help you get started. Happy coding!
Thank you for this, will see what i can do
As barely a beginner coder, after trying & learning this for a while I found it’s too advanced for me. SDK AI assistant gave the following code. Nothing has worked yet. Can anyone help fix this?
import { orders } from "@wix/pricing-plans";
export async function getActiveOrders() {
try {
const options = {
orderStatuses: ['ACTIVE'], // Filter for active orders
};
const ordersList = await orders.managementListOrders(options); //!error under options
return ordersList.orders.map(order => order.buyer.memberId); // Extract member IDs
} catch (error) {
console.error(error);
}
}
$w.onReady(async function () {
const memberIds = await getActiveOrders(); // Get member IDs with active plans
const filter = wixData.filter().hasSome('_owner', memberIds); // Adjust 'ownerId' to match your dataset field
$w('#listingsDataset').setFilter(filter);
});
I haven’t started using the SDK syntax yet, but from what I can see, the managementListOrders() function requires elevated and here’s how you would set it up using Velo:
fetchOrders.jsw (backend):
import { orders } from 'wix-pricing-plans.v2';
import * as wixAuth from 'wix-auth';
export async function elevatedManagementListOrders(options) {
const elevatedOrders = wixAuth.elevate(orders.managementListOrders);
try {
const result = await elevatedOrders(options);
return result.orders;
} catch (error) {
console.log(error);
}
}
Page Code (frontend):
import wixData from 'wix-data';
import { elevatedManagementListOrders } from 'backend/fetchOrders'
$w.onReady(async function () {
let options = {
orderStatuses: ["ACTIVE"]
}
var activeOrderList = await elevatedManagementListOrders(options);
// Implement your data filtering logic here
});