Search.../

getPlan( )

Retrieves a pricing plan by the specified ID.

Description

The getPlan() function returns a Promise that resolves to a plan whose ID matched the specified ID.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function getPlan(_id: string): Promise<Plan>

getPlan Parameters

NAME
TYPE
DESCRIPTION
_id
string

Plan ID.

Returns

Fulfilled - The retrieved plan's information.

Return Type:

Promise<
Plan
>
NAME
TYPE
DESCRIPTION
_createdDate
Date

Date plan was created.

_id
string

Plan ID.

_updatedDate
Date

Date plan was last updated.

allowFutureStartDate
boolean

Whether the buyer can start the plan at a later date.

Default: false.

archived
boolean

Whether the plan is archived. Archived plans are not visible and can't be purchased anymore, but existing purchases remain in effect.

buyerCanCancel
boolean

Whether the buyer is allowed to cancel their plan. If false, calling the cancelOrder() function returns an error.

Default: false.

description
string

Plan description.

formId
string

ID of the form associated with the plan at checkout.

hasOrders
boolean

Whether the plan has any orders (including pending and unpaid orders).

maxPurchasesPerBuyer
number

Number of times the same buyer can purchase the plan. Currently limited to support:

  • Empty value or a value of 0, meaning no limitation.
  • Value of 1, meaning limited to one purchase per buyer.
name
string

Plan name.

perks
StringList

List of text strings that promote what is included with this plan.

For example, "Plenty of parking" or "Free gift on your birthday".

pricing
Pricing

Plan price, payment schedule, and expiration.

primary
boolean

Whether the plan is marked as primary. If true, the plan is highlighted on the site with a custom ribbon.

Default: false.

public
boolean

Whether the plan is public (visible to site visitors and members).

slug
string

URL-friendly version of plan name. Unique across all plans in the same site.

termsAndConditions
string

Any terms and conditions that apply to the plan. This information will be displayed during checkout.

Was this helpful?

Get a plan (dashboard page code)

Copy Code
1import { plans} from 'wix-pricing-plans.v2';
2
3/* Sample _id value: '838f2c9d-c8d0-4799-a10a-e2f23849db10' */
4
5export async function myGetPlanFunction(_id) {
6 try {
7 const myPlan = await plans.getPlan(_id);
8
9 return myPlan;
10 } catch (error) {
11 console.error(error);
12 // Handle the error
13 }
14}
15
16/* Promise resolves to:
17 * {
18 * "_createdDate": "2024-01-07T07:28:42.863Z",
19 * "_id": "838f2c9d-c8d0-4799-a10a-e2f23849db10",
20 * "_updatedDate": "2024-01-07T08:36:07.520Z",
21 * "allowFutureStartDate": false,
22 * "archived": false,
23 * "buyerCanCancel": true,
24 * "description": "Complete with all features. One month free trial.",
25 * "formId": "ee62cefa-bdc2-4b5d-baab-6faeef83cecb",
26 * "hasOrders": false,
27 * "maxPurchasesPerBuyer": 0,
28 * "name": "Premium Plan - annual - 30 day trial",
29 * "perks": {
30 * "values": [
31 * "Unlimited video library streaming access",
32 * "File sharing enabled for all channels"
33 * ]
34 * },
35 * "pricing": {
36 * "freeTrialDays": 30,
37 * "price": {
38 * "currency": "EUR",
39 * "value": "500"
40 * },
41 * "subscription": {
42 * "cycleCount": 2,
43 * "cycleDuration": {
44 * "count": 1,
45 * "unit": "YEAR"
46 * }
47 * }
48 * },
49 * "primary": false,
50 * "public": true,
51 * "slug": "premium-plan-annual-30-day-trial-1",
52 * "termsAndConditions": "Unlimited usage of services, subject to Fair Usage and Code of Conduct policies."
53 * }
54 */
Get a plan (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { plans } from 'wix-pricing-plans.v2';
3import { elevate } from 'wix-auth';
4
5/* Sample _id value: '838f2c9d-c8d0-4799-a10a-e2f23849db10' */
6
7export const myGetPlanFunction = webMethod(Permissions.Anyone, async (_id) => {
8 try {
9 const elevatedGetPlan = elevate(plans.getPlan);
10 const myPlan = await elevatedGetPlan(_id);
11
12 return myPlan;
13 } catch (error) {
14 console.error(error);
15 // Handle the error
16 }
17});
18
19/* Promise resolves to:
20 * {
21 * "_createdDate": "2024-01-07T07:28:42.863Z",
22 * "_id": "838f2c9d-c8d0-4799-a10a-e2f23849db10",
23 * "_updatedDate": "2024-01-07T08:36:07.520Z",
24 * "allowFutureStartDate": false,
25 * "archived": false,
26 * "buyerCanCancel": true,
27 * "description": "Complete with all features. One month free trial.",
28 * "formId": "ee62cefa-bdc2-4b5d-baab-6faeef83cecb",
29 * "hasOrders": false,
30 * "maxPurchasesPerBuyer": 0,
31 * "name": "Premium Plan - annual - 30 day trial",
32 * "perks": {
33 * "values": [
34 * "Unlimited video library streaming access",
35 * "File sharing enabled for all channels"
36 * ]
37 * },
38 * "pricing": {
39 * "freeTrialDays": 30,
40 * "price": {
41 * "currency": "EUR",
42 * "value": "500"
43 * },
44 * "subscription": {
45 * "cycleCount": 2,
46 * "cycleDuration": {
47 * "count": 1,
48 * "unit": "YEAR"
49 * }
50 * }
51 * },
52 * "primary": false,
53 * "public": true,
54 * "slug": "premium-plan-annual-30-day-trial-1",
55 * "termsAndConditions": "Unlimited usage of services, subject to Fair Usage and Code of Conduct policies."
56 * }
57 */
Duplicate a plan

The populatePlansDropdown function retrieves all plans and then populates them into the #plansDropdown. The #plansDropdown and #clonePlanBtn are set to disabled by default.

Copy Code
1/*************************************
2 * Backend code - plan-functions.web.js *
3 *************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { plans } from 'wix-pricing-plans.v2';
7import { elevate } from 'wix-auth';
8
9const elevatedListPlans = elevate(plans.listPlans);
10const elevatedGetPlan = elevate(plans.getPlan);
11const elevatedCreatePlan = elevate(plans.createPlan);
12
13export const listPlans = webMethod(Permissions.Anyone, async () => {
14 try {
15 const response = await elevatedListPlans();
16 const allPlans = response.plans;
17
18 return allPlans;
19 } catch (error) {
20 console.error(error);
21 // Handle the error
22 }
23});
24
25export const getPlan = webMethod(Permissions.Anyone, async (planId) => {
26 try {
27 const selectedPlan = await elevatedGetPlan(planId);
28
29 return selectedPlan;
30 } catch (error) {
31 console.error(error);
32 // Handle the error
33 }
34});
35
36export const createPlan = webMethod(Permissions.Anyone, async (plan) => {
37 try {
38 const newPlan = await elevatedCreatePlan(plan);
39
40 return newPlan;
41 } catch (error) {
42 console.error(error);
43 // Handle the error
44 }
45});
46
47
48/*************
49 * Page code *
50 *************/
51
52import { listPlans, getPlan, createPlan } from 'backend/plan-functions.web';
53$w.onReady(async function () {
54 $w('#plansDropdown').disable();
55 $w('#clonePlanBtn').disable();
56 populatePlansDropdown();
57
58 let selectedPlan;
59
60 $w('#plansDropdown').onChange(async () => {
61 const planId = $w('#plansDropdown').value;
62 selectedPlan = await getPlan(planId);
63
64 // Set default value for the new plan name
65 $w('#planName').value = `${selectedPlan.name}-duplicate`
66 $w('#clonePlanBtn').enable();
67 });
68
69 $w('#clonePlanBtn').onClick(async () => {
70 const newPlan = selectedPlan;
71 // Use inputs to set the new plan name and price
72 newPlan.name = $w('#planName').value;
73 newPlan.pricing.price.value = $w('#price').value;
74
75 await createPlan(newPlan);
76 });
77});
78
79async function populatePlansDropdown() {
80 const plans = await listPlans();
81 $w('#plansDropdown').options = plans.map((plan) => {
82 return {
83 label: plan.name,
84 value: plan._id
85 }
86 });
87 $w('#plansDropdown').enable();
88}