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

Copy Code
1import { plans} from 'wix-pricing-plans.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample _id value: '838f2c9d-c8d0-4799-a10a-e2f23849db10' */
5
6export async function myGetPlanFunction(_id) {
7 try {
8 const elevatedGetPlan = elevate(plans.getPlan);
9 const myPlan = await elevatedGetPlan(_id);
10
11 return myPlan;
12 } catch (error) {
13 console.error(error);
14 // Handle the error
15 }
16}
17
18/* Promise resolves to:
19 * {
20 * "_createdDate": "2024-01-07T07:28:42.863Z",
21 * "_id": "838f2c9d-c8d0-4799-a10a-e2f23849db10",
22 * "_updatedDate": "2024-01-07T08:36:07.520Z",
23 * "allowFutureStartDate": false,
24 * "archived": false,
25 * "buyerCanCancel": true,
26 * "description": "Complete with all features. One month free trial.",
27 * "formId": "ee62cefa-bdc2-4b5d-baab-6faeef83cecb",
28 * "hasOrders": false,
29 * "maxPurchasesPerBuyer": 0,
30 * "name": "Premium Plan - annual - 30 day trial",
31 * "perks": {
32 * "values": [
33 * "Unlimited video library streaming access",
34 * "File sharing enabled for all channels"
35 * ]
36 * },
37 * "pricing": {
38 * "freeTrialDays": 30,
39 * "price": {
40 * "currency": "EUR",
41 * "value": "500"
42 * },
43 * "subscription": {
44 * "cycleCount": 2,
45 * "cycleDuration": {
46 * "count": 1,
47 * "unit": "YEAR"
48 * }
49 * }
50 * },
51 * "primary": false,
52 * "public": true,
53 * "slug": "premium-plan-annual-30-day-trial-1",
54 * "termsAndConditions": "Unlimited usage of services, subject to Fair Usage and Code of Conduct policies."
55 * }
56 */
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.jsw *
3 *************************************/
4
5import { plans } from 'wix-pricing-plans.v2';
6import { elevate } from 'wix-auth';
7
8const elevatedListPlans = elevate(plans.listPlans);
9const elevatedGetPlan = elevate(plans.getPlan);
10const elevatedCreatePlan = elevate(plans.createPlan);
11
12export async function listPlans() {
13 try {
14 const response = await elevatedListPlans();
15 const allPlans = response.plans;
16
17 return allPlans;
18 } catch (error) {
19 console.error(error);
20 // Handle the error
21 }
22}
23
24export async function getPlan(planId) {
25 try {
26 const selectedPlan = await elevatedGetPlan(planId);
27
28 return selectedPlan;
29 } catch (error) {
30 console.error(error);
31 // Handle the error
32 }
33}
34
35export async function createPlan(plan) {
36 try {
37 const newPlan = await elevatedCreatePlan(plan);
38
39 return newPlan;
40 } catch (error) {
41 console.error(error);
42 // Handle the error
43 }
44}
45
46
47/*************
48 * Page code *
49 *************/
50
51import { listPlans, getPlan, createPlan } from 'backend/plan-functions';
52$w.onReady(async function () {
53 $w('#plansDropdown').disable();
54 $w('#clonePlanBtn').disable();
55 populatePlansDropdown();
56
57 let selectedPlan;
58
59 $w('#plansDropdown').onChange(async () => {
60 const planId = $w('#plansDropdown').value;
61 selectedPlan = await getPlan(planId);
62
63 // Set default value for the new plan name
64 $w('#planName').value = `${selectedPlan.name}-duplicate`
65 $w('#clonePlanBtn').enable();
66 });
67
68 $w('#clonePlanBtn').onClick(async () => {
69 const newPlan = selectedPlan;
70 // Use inputs to set the new plan name and price
71 newPlan.name = $w('#planName').value;
72 newPlan.pricing.price.value = $w('#price').value;
73
74 await createPlan(newPlan);
75 });
76});
77
78async function populatePlansDropdown() {
79 const plans = await listPlans();
80 $w('#plansDropdown').options = plans.map((plan) => {
81 return {
82 label: plan.name,
83 value: plan._id
84 }
85 });
86 $w('#plansDropdown').enable();
87}