I’m trying to run the script below using the ecom-custom-discount-trigger SPI to apply a discount on all products when the current logged in user is a paid member (has a subscription plan) on my website.
I know it is a paid member when one of their member roles (currentMember.getRoles()) has the same id as the member role I created on my dashboard specifically for this purpose.
But it actually does not return anything from the current member, as if there was no logged in user. Obviously I tried it on production and logged in with a test account, but the log says the function is no returning any logged user.
I’m new to javascript, so any help would be very much appreciated. I actually don’t what else to try.
Thanks!
import * as ecomDiscountsTrigger from 'interfaces-ecommerce-v1-custom-trigger';
import { currentMember } from 'wix-members-backend';
export const listTriggers = async (context) => {
const result = {
customTriggers: [{
_id: "1",
name: "Paid Member"
}]
}
console.log('result', result)
return result
};
export const getEligibleTriggers = async (options, context) => {
const result = getRoleIds()
.then((roles) => {
if (roles.includes("ad3ec1fe-6529-485a-ac53-68ec63ce687b")) {
return {
eligibleTriggers: [{
customTriggerId: "1",
identifier: options.triggers[0].identifier
}]
}
} else {
return {
eligibleTriggers: []
}
}
})
.catch(() => {
return {
eligibleTriggers: []
}
})
return result
};
export async function getRoleIds() {
return currentMember.getRoles()
.then((roles) => {
return roles.map(role => role._id)
})
.catch((error) => {
console.error(error);
return [""]
});
}