Search.../

createPlan( )

Creates a pricing plan.

Description

The createPlan() function returns a Promise that resolves to a newly-created pricing plan after is has successfully been created.

The passed plan object must contain a pricing model. A pricing model can be one of the following:

  • A subscription: A subscription with recurring payment and how often the plan occurs. Subscriptions can have free trial days.
  • A plan that does not renew: A single payment for a specific duration that doesn't renew.
  • An unlimited plan: A single payment for an unlimited amount of time (until canceled).

Pricing plans created by this function are available to the site owner in the Pricing Plans section in the Dashboard.

Admin Method

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

Syntax

function createPlan(plan: Plan): Promise<Plan>

createPlan Parameters

NAME
TYPE
DESCRIPTION
plan
Plan

Information for the plan being created.

Returns

Fulfilled - The created plan.

Rejected - Error message.

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?

Create a simple plan

Copy Code
1import { plans } from 'wix-pricing-plans.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample plan object:
5 * {
6 * name: 'Default',
7 * pricing: {
8 * singlePaymentUnlimited: true
9 * }
10 * }
11 */
12
13export async function myCreatePlanFunction(plan) {
14 try {
15 const elevatedCreatePlan = elevate(plans.createPlan);
16 const newPlan = await elevatedCreatePlan(plan);
17
18 return newPlan;
19 } catch (error) {
20 console.error(error);
21 // Handle the error
22 }
23}
24
25/* Promise resolves to:
26 * {
27 * "_createdDate": "2024-01-16T10:32:05.818Z",
28 * "_id": "aa0d8e0e-99ad-4c95-ac48-4955e37956c5",
29 * "_updatedDate": "2024-01-16T10:32:05.818Z",
30 * "allowFutureStartDate": false,
31 * "archived": false,
32 * "buyerCanCancel": true,
33 * "description": "",
34 * "hasOrders": false,
35 * "name": "Default",
36 * "perks": {
37 * "values": []
38 * },
39 * "pricing": {
40 * "price": {
41 * "currency": "EUR",
42 * "value": "0"
43 * },
44 * "singlePaymentUnlimited": true
45 * },
46 * "primary": false,
47 * "public": true,
48 * "slug": "default"
49 * }
50 */
51
Create a plan with optional fields

Copy Code
1import { plans } from 'wix-pricing-plans.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample plan value:
5 * {
6 * allowFutureStartDate: true,
7 * buyerCanCancel: true,
8 * description: 'Fun content for our younger users',
9 * formId: 'ee62cefa-bdc2-4b5d-baab-6faeef83cecb',
10 * maxPurchasesPerBuyer: 1,
11 * name: 'Junior',
12 * perks: {
13 * values: [
14 * 'Multiplayer',
15 * 'Multiple devices',
16 * 'No ads",
17 * 'Unlimited access'
18 * ]
19 * },
20 * pricing: {
21 * price: {
22 * currency: 'USD',
23 * value: '10.00"
24 * },
25 * subscription: {
26 * cycleCount: 12,
27 * cycleDuration: {
28 * count: 1,
29 * unit: 'MONTH'
30 * }
31 * }
32 * },
33 * public: true,
34 * termsAndConditions: 'No sharing access with others!'
35 * }
36 */
37
38export async function myCreatePlanFunction(plan) {
39 try {
40 const elevatedCreatePlan = elevate(plans.createPlan);
41 const newPlan = await elevatedCreatePlan(plan);
42
43 const planId = newPlan._id;
44 const pricingObject = newPlan.pricing;
45 const perks = newPlan.perks;
46
47 return newPlan;
48 } catch (error) {
49 console.error(error);
50 // Handle the error
51 }
52}
53
54/* Promise resolves to:
55 * {
56 * "_createdDate": "2024-01-21T07:06:06.993Z",
57 * "_id": "046371ff-7e5d-4bdb-a01d-a00ff462bed8",
58 * "_updatedDate": "2024-01-21T07:06:06.993Z",
59 * "allowFutureStartDate": true,
60 * "archived": false,
61 * "buyerCanCancel": true,
62 * "description": "Fun content for our younger users",
63 * "formId": "ee62cefa-bdc2-4b5d-baab-6faeef83cecb",
64 * "hasOrders": false,
65 * "maxPurchasesPerBuyer": 1,
66 * "name": "Junior",
67 * "perks": {
68 * "values": [
69 * "Multiplayer",
70 * "Multiple devices",
71 * "No ads",
72 * "Unlimited access"
73 * ]
74 * },
75 * "pricing": {
76 * "price": {
77 * "currency": "USD",
78 * "value": "10.00"
79 * },
80 * "public": true,
81 * "slug": "junior",
82 * "subscription": {
83 * "cycleCount": 12,
84 * "cycleDuration": {
85 * "count": 1,
86 * "unit": "MONTH"
87 * }
88 * },
89 * "termsAndConditions": "No sharing access with others!"
90 * }
91 * }
92 */
93
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}