diff --git a/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js b/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js index ca913baf5..fb002ebdf 100644 --- a/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js +++ b/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js @@ -21,25 +21,7 @@ class SingleProductActionHandler { this.cartHelper = null; } - getPlanIdFromVariation = (variation) => { - let subscription_plan = ''; - PayPalCommerceGateway.variable_paypal_subscription_variations.forEach((element) => { - let obj = {}; - variation.forEach(({name, value}) => { - Object.assign(obj, {[name.replace('attribute_', '')]: value}); - }) - - if(JSON.stringify(obj) === JSON.stringify(element.attributes) && element.subscription_plan !== '') { - subscription_plan = element.subscription_plan; - } - }); - - return subscription_plan; - } - - subscriptionsConfiguration(selected_variation = null) { - const subscription_plan = selected_variation !== null ? this.getPlanIdFromVariation(selected_variation) : this.config.subscription_plan_id - + subscriptionsConfiguration(subscription_plan) { return { createSubscription: (data, actions) => { return actions.subscription.create({ diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index 61f167d31..f76ba9c91 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -3,6 +3,7 @@ import SingleProductActionHandler from "../ActionHandler/SingleProductActionHand import {hide, show} from "../Helper/Hiding"; import BootstrapHelper from "../Helper/BootstrapHelper"; import {loadPaypalJsScript} from "../Helper/ScriptLoading"; +import {getPlanIdFromVariation} from "../Helper/Subscriptions" class SingleProductBootstap { constructor(gateway, renderer, messages, errorHandler) { @@ -165,6 +166,14 @@ class SingleProductBootstap { ) { const buttonWrapper = document.getElementById('ppc-button-ppcp-gateway'); buttonWrapper.innerHTML = ''; + + const subscription_plan = this.variations() !== null + ? getPlanIdFromVariation(this.variations()) + : PayPalCommerceGateway.subscription_plan_id + if(!subscription_plan) { + return; + } + loadPaypalJsScript( { clientId: PayPalCommerceGateway.client_id, @@ -172,7 +181,7 @@ class SingleProductBootstap { intent: 'subscription', vault: true }, - actionHandler.subscriptionsConfiguration(this.variations()), + actionHandler.subscriptionsConfiguration(subscription_plan), this.gateway.button.wrapper ); return; diff --git a/modules/ppcp-button/resources/js/modules/Helper/Subscriptions.js b/modules/ppcp-button/resources/js/modules/Helper/Subscriptions.js index a205ac459..4366fd9d2 100644 --- a/modules/ppcp-button/resources/js/modules/Helper/Subscriptions.js +++ b/modules/ppcp-button/resources/js/modules/Helper/Subscriptions.js @@ -2,3 +2,19 @@ export const isChangePaymentPage = () => { const urlParams = new URLSearchParams(window.location.search) return urlParams.has('change_payment_method'); } + +export const getPlanIdFromVariation = (variation) => { + let subscription_plan = ''; + PayPalCommerceGateway.variable_paypal_subscription_variations.forEach((element) => { + let obj = {}; + variation.forEach(({name, value}) => { + Object.assign(obj, {[name.replace('attribute_', '')]: value}); + }) + + if(JSON.stringify(obj) === JSON.stringify(element.attributes) && element.subscription_plan !== '') { + subscription_plan = element.subscription_plan; + } + }); + + return subscription_plan; +}