I know I can assign members manually, but is there a way to do this automatically? I need to do this so only paid members of a certain product can access a certain page.
Hi! Currently, it is not possible to assign a role to member through code, you can find more information in this Forum discussion: https://www.wix.com/corvid/forum/community-discussion/set-member-role-using-wix-code
This has been updated: https://www.wix.com/velo/reference/wix-users-backend/roles-obj/assignrole
Hi!
I am trying to extract the best of Wix integration in Subscriptions Dashboard from “Paid Plans” and “Wix Stores”.
“Paid Plans” has a very nice native feature to control page access to members, what “Wix Stores” subscriptions does not. On the other hand, “Wix Stores” has several other interesting marketing features, like coupons and different ways to set price according to the subscription time (or even for single payment).
My ideia is to grant access to some pages to members when a product (by subscription) is purchased in “Wix Stores”. I am trying to do this by assigning a role to the member, which correspond to the pages’ access. Here is the backend code:
// in events.js
import wixStores from 'wix-stores-backend'
import wixUsersBackend from 'wix-users-backend'
import {roles} from 'wix-users-backend';
export function wixStores_onOrderPaid(event) {
// ID dos produtos na WixStores
const productidbasico = "58d4eb34-...-c14e0d2c0619"
const productidcontrole = "72dc9d0f-...-5e29c1921a9a"
const productidenterprise = "746453d4-...-f1224d2bb65e"
// ID dos Member's Roles
const RoleIdBasico = "6d648993-...-373e365940eb"
const RoleIdControle = "0b661d48-...-6ff18dc89f22"
const RoleIdEnterprise = "fdc0c708-...-e14ef9fe4b6f"
const memberId = event.buyerInfo.id;
const productId = event.lineItems[0].productId;
if (productId === productidbasico) {
let roleId = RoleIdBasico
assignRole(roleId, memberId)
}
if (productId === productidcontrole) {
let roleId = RoleIdControle
assignRole(roleId, memberId)
}
if (productId === productidenterprise) {
let roleId = RoleIdEnterprise
assignRole(roleId, memberId)
}
}
export function assignRole(roleId, memberId) {
return roles.assignRole(roleId, memberId, { suppressAuth: true })
.then( () => {
console.log("Role assigned to member");
})
.catch((error) => {
console.log(error);
});
}
The website is: www.2fee.com.br
Problems:
- It is not working, roles are not assigned
- It is hard to test, it is only available by publishing the site and making a purchase with real money
I have tested assigning a role to the member manually (Dashboard) and it did grant access to the page. But not using this code…
I have also tried calling the function “assignRole” in the “Thank You” frontend page, but didn’t work as well (and I am concerned about security breaches).
Ideas, please!
Thanks.
@yisrael-wix you are the guy, any hint on this? I owe you a beer when you come to Brazil, a good craft one!
When your assignRole() function runs, do you get “Role assigned to member” in the console, or the error message? If you don’t get either message, then maybe the assignRole() isn’t being called.
Did you get this working? Still having problems?
I’ve been playing with roles and see it working. Perhaps you have other issues?
Hi @yisrael-wix ,
Thanks for your support! I am sorry, I am newbie in Wix and JS.
Still not working as is. I have tested the syntax of function “wixStores_onOrderPaid()” and it seems that it is returning the correct parameters used later in “assignRole()”
const memberId = event.buyerInfo.id;
const productId = event.lineItems[0].productId;
Actually, how can I see the console.log if the function is only called in backend? What would you suggest to test it other way?
Cheers.
@contato87032 You can see the console.log() output in the browser’s console. For example, to get the console in Safari:
@yisrael-wix , thank you again!
Wix is cancelling my test payments (real money, but small amounts), due to fraud suspicion. It tried to use discount coupons to deduct the full value of the products, but it seems that Wix does not allow that either (it prompts an error during payment).
I am trying another approach, I will be back soon with it.
Cheers.
@yisrael-wix , just to let you know that I fixed my code error. Now it is working properly… thank you again!
Just one more thing: I noticed that in “Plans” collection from “Paid Plans” there is a “roleId” field. I am trying to figure out what happens if I assign this role to a member… Does it affect the subscriptions context or Member’s Permissions Dashboard?
Hey man!I have the exact same challenge as you had. I am missing functionality in vanilla Pricing Plans. I want to be able to assign role on product purchase automatically with 3 different roles.
Is there a possibility that you would share your working code here?
It would be greatly appreciated since I am green as hell when it comes to coding.
Thanks.
Hi @apocalyptan !
I am also newbie.
Here is the backend code. Definitely it is not elegant, but for 3 simple products and plans it works fine:
import wixStores from 'wix-stores-backend';
import wixPaidPlans from 'wix-paid-plans';
import wixUsersBackend from 'wix-users-backend';
import {roles} from 'wix-users-backend';
export async function wixStores_onOrderPaid(event) {
// WixStores Product IDs
const productidbasico = "58d4eb34-...-c14e0d2c0619"
const productidcontrole = "72dc9d0f-...-5e29c1921a9a"
const productidenterprise = "746453d4-...-f1224d2bb65e"
// Member Role IDs
const RoleIdBasico = "6d648993-...-373e365940eb"
const RoleIdControle = "0b661d48-...-6ff18dc89f22"
const RoleIdEnterprise = "fdc0c708-...-e14ef9fe4b6f"
const memberId = event.buyerInfo.id;
const productId = event.lineItems[0].productId;
if (productId === productidbasico) {
let roleId = RoleIdBasico
assignRole(roleId, memberId)
}
if (productId === productidcontrole) {
let roleId = RoleIdControle
assignRole(roleId, memberId)
}
if (productId === productidenterprise) {
let roleId = RoleIdEnterprise
assignRole(roleId, memberId)
}
}
export async function assignRole(planId, memberId) {
// Plan IDs
const planIdBasico = "ef0fca3b-...-4e88e82add7b"
const planIdControle = "dd49daff-...-6ba743da6c2d"
const planIdEnterprise = "dcb2b8f3-...-b7c21bc2d1e7"
function planRole(pId){
if (pId === planIdBasico) {
let RoleIdBasico = planIdBasico
return RoleIdBasico
}
if (pId === planIdControle) {
let RoleIdControle = planIdControle
return RoleIdControle
}
if (pId === planIdEnterprise) {
let RoleIdEnterprise = planIdEnterprise
return RoleIdEnterprise
}
}
let options = {
"suppressAuth": true,
"suppressHooks": true
};
let roleId = await planRole(planId)
return roles.assignRole(roleId, memberId, options)
.then( () => {
return true
})
.catch((error) => {
return error
});
}
Cheers!
Can someone please help me with this and DM me @nonchbeauty
Welcome to the Velo Forum. This forum is here for you to get help when you encounter difficulties while coding your site, not to ask others to provide free code services for you. Read more about our community and its guidelines so you will know what to expect.
If you are interested in hiring a developer, please visit the Wix Marketplace .