Skip to content

JS error in single product when free trial subscription in the cart (2783) #2098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/ppcp-button/resources/js/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ document.addEventListener(
if (!typeof (PayPalCommerceGateway)) {
console.error('PayPal button could not be configured.');
return;
return;
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import {hide, show} from "../Helper/Hiding";
import BootstrapHelper from "../Helper/BootstrapHelper";
import {loadPaypalJsScript} from "../Helper/ScriptLoading";
import {getPlanIdFromVariation} from "../Helper/Subscriptions"
import SimulateCart from "../Helper/SimulateCart";
import {strRemoveWord, strAddWord, throttle} from "../Helper/Utils";
import merge from "deepmerge";
import {throttle} from "../Helper/Utils";
import CartSimulator from "../Helper/CartSimulator";

class SingleProductBootstap {
constructor(gateway, renderer, errorHandler) {
Expand Down Expand Up @@ -217,71 +216,7 @@ class SingleProductBootstap {
}

simulateCart() {
if (!this.gateway.simulate_cart.enabled) {
return;
}

const actionHandler = new SingleProductActionHandler(
null,
null,
this.form(),
this.errorHandler,
);

const hasSubscriptions = PayPalCommerceGateway.data_client_id.has_subscriptions
&& PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled;

const products = hasSubscriptions
? actionHandler.getSubscriptionProducts()
: actionHandler.getProducts();

(new SimulateCart(
this.gateway.ajax.simulate_cart.endpoint,
this.gateway.ajax.simulate_cart.nonce,
)).simulate((data) => {

jQuery(document.body).trigger('ppcp_product_total_updated', [data.total]);

let newData = {};
if (typeof data.button.is_disabled === 'boolean') {
newData = merge(newData, {button: {is_disabled: data.button.is_disabled}});
}
if (typeof data.messages.is_hidden === 'boolean') {
newData = merge(newData, {messages: {is_hidden: data.messages.is_hidden}});
}
if (newData) {
BootstrapHelper.updateScriptData(this, newData);
}

if ( this.gateway.single_product_buttons_enabled !== '1' ) {
return;
}

let enableFunding = this.gateway.url_params['enable-funding'];
let disableFunding = this.gateway.url_params['disable-funding'];

for (const [fundingSource, funding] of Object.entries(data.funding)) {
if (funding.enabled === true) {
enableFunding = strAddWord(enableFunding, fundingSource);
disableFunding = strRemoveWord(disableFunding, fundingSource);
} else if (funding.enabled === false) {
enableFunding = strRemoveWord(enableFunding, fundingSource);
disableFunding = strAddWord(disableFunding, fundingSource);
}
}

if (
(enableFunding !== this.gateway.url_params['enable-funding']) ||
(disableFunding !== this.gateway.url_params['disable-funding'])
) {
this.gateway.url_params['enable-funding'] = enableFunding;
this.gateway.url_params['disable-funding'] = disableFunding;
jQuery(this.gateway.button.wrapper).trigger('ppcp-reload-buttons');
}

this.handleButtonStatus(false);

}, products);
(new CartSimulator(this)).simulate();
}
}

Expand Down
95 changes: 95 additions & 0 deletions modules/ppcp-button/resources/js/modules/Helper/CartSimulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler";
import SimulateCart from "./SimulateCart";
import BootstrapHelper from "./BootstrapHelper";
import {strAddWord, strRemoveWord} from "./Utils";
import merge from "deepmerge";

class CartSimulator {

constructor(bootstrap) {
this.bootstrap = bootstrap;
}

/**
* @return
*/
simulate() {
const gatewaySettings = this.bootstrap.gateway;

if (!gatewaySettings.simulate_cart.enabled) {
return;
}

const actionHandler = new SingleProductActionHandler(
null,
null,
this.bootstrap.form(),
this.bootstrap.errorHandler,
);

const hasSubscriptions = PayPalCommerceGateway.data_client_id.has_subscriptions
&& PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled;

const products = hasSubscriptions
? actionHandler.getSubscriptionProducts()
: actionHandler.getProducts();

(new SimulateCart(
gatewaySettings.ajax.simulate_cart.endpoint,
gatewaySettings.ajax.simulate_cart.nonce,
)).simulate((data) => {

// Trigger event with simulated total.
jQuery(document.body).trigger('ppcp_product_total_updated', [data.total]);

// Hide and show fields.
let newData = {};
if (typeof data.button.is_disabled === 'boolean') {
newData = merge(newData, {button: {is_disabled: data.button.is_disabled}});
}
if (typeof data.messages.is_hidden === 'boolean') {
newData = merge(newData, {messages: {is_hidden: data.messages.is_hidden}});
}
if (newData) {
BootstrapHelper.updateScriptData(this.bootstrap, newData);
}

// Check if single product buttons enabled.
if ( gatewaySettings.single_product_buttons_enabled !== '1' ) {
return;
}

// Update funding sources.
let enableFunding = gatewaySettings.url_params['enable-funding'] || '';
let enableFundingBefore = enableFunding;

let disableFunding = gatewaySettings.url_params['disable-funding'] || '';
let disableFundingBefore = disableFunding;

for (const [fundingSource, funding] of Object.entries(data.funding)) {
if (funding.enabled === true) {
enableFunding = strAddWord(enableFunding, fundingSource);
disableFunding = strRemoveWord(disableFunding, fundingSource);
} else if (funding.enabled === false) {
enableFunding = strRemoveWord(enableFunding, fundingSource);
disableFunding = strAddWord(disableFunding, fundingSource);
}
}

// Detect and update funding changes and reload buttons.
if (
(enableFunding !== enableFundingBefore) ||
(disableFunding !== disableFundingBefore)
) {
gatewaySettings.url_params['enable-funding'] = enableFunding;
gatewaySettings.url_params['disable-funding'] = disableFunding;
jQuery(gatewaySettings.button.wrapper).trigger('ppcp-reload-buttons');
}

this.bootstrap.handleButtonStatus(false);

}, products);
}
}

export default CartSimulator;