Skip to content

Commit

Permalink
Merge pull request #76 from MaximilianoRicoTabo/enhancement/direct-li…
Browse files Browse the repository at this point in the history
…nk-to-plan

[ENHANCEMENT] #62 Add support to pass payment plan as an URL param
  • Loading branch information
andrewlimaza authored Jan 8, 2025
2 parents 27b3a75 + 18fb3f7 commit 440458f
Showing 1 changed file with 91 additions and 55 deletions.
146 changes: 91 additions & 55 deletions js/frontend.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,100 @@
jQuery(document).ready(function () {
jQuery(document).ready(function ($) {

if (jQuery("#pmpropp_payment_plans").length > 0) {

jQuery.each(payment_plans.plans, function (key, val) {

jQuery("#pmpropp_payment_plans").append(val.html);

//Make sure the level cost text applies to the selected level
if (val.default == "yes") {
// Bail if no payment plans are present
if ( payment_plans == undefined || payment_plans.plans.length === 0 ) {
return;
}

var data = {
action: 'pmpropp_request_price_change',
pmpro_level: payment_plans.parent_level,
plan: val.id
}
// Are there any payment plans in the URL?
const urlParams = new URLSearchParams( window.location.search );
const payment_plan_query = urlParams.get( 'pmpropp_chosen_plan' );

jQuery.post(payment_plans.ajaxurl, data, function (response) {
if (response !== '') {
jQuery("#pmpro_level_cost").html(response);
}
});
// Get the default payment plan
const default_plan = payment_plans.plans.find( plan => plan.default === "yes" && plan.id !== payment_plans.parent_level );

}
// Append the payment plans to the DOM
payment_plans.plans.forEach( element => {
$( "#pmpropp_payment_plans" ).append( element.html );
});

});

if (typeof localStorage !== 'undefined') {
var chosen_plan = localStorage.getItem('pmpropp_chosen_plan');
if (chosen_plan !== "") {
jQuery("#pmpropp_chosen_plan_choice_" + chosen_plan).click();
}
// If there is a payment plan in the URL, select it and ignore the default
if ( payment_plan_query) {
// Check if value is a number
if ( /^\d+$/.test( payment_plan_query ) ) {
appendPlanAndPriceByNumberId( payment_plan_query );
} else {
appendPlanAndPriceByPlanId( payment_plan_query );
}
// Apply the default plan if there is no plan in the URL
} else if ( default_plan ) {
appendPlanAndPrice( default_plan );
// No default plan, no plan in the URL, just select the plan from the local storage (last selected plan) if it
//exists, otherwise do nothing and parent level regular payment plan will be selected
} else {
const chosen_plan = localStorage.getItem( 'pmpropp_chosen_plan' );
if ( chosen_plan !== "" ) {
appendPlanAndPriceByPlanId( chosen_plan );
}

jQuery("body").on("click", ".pmpropp_chosen_plan", function () {

var value = jQuery(this).val();

jQuery.each(payment_plans.plans, function (key, val) {

if (val.id == value) {

var data = {
action: 'pmpropp_request_price_change',
pmpro_level: payment_plans.parent_level,
plan: value
}

jQuery.post(payment_plans.ajaxurl, data, function (response) {
if (response !== '') {
if (typeof localStorage !== 'undefined') {
localStorage.setItem('pmpropp_chosen_plan', value);
}
jQuery("#pmpro_level_cost").html(response);
}
});
}
});

});

}

});
$( ".pmpropp_chosen_plan" ).on( "click", function( ) {
const planId = $(this).val();
appendPlanAndPriceByPlanId( planId );
});

});

/**
* Append the plan and price by the numberId (2)
*
* @param {number} numberId Just the int. The plan id is constructed as 'L-' + payment_plans.parent_level + '-P-' + numberId
* @since TBD
* @return {void}
*/
const appendPlanAndPriceByNumberId = ( numberId ) => {
const planId = 'L-' + payment_plans.parent_level + '-P-' + numberId;
appendPlanAndPriceByPlanId( planId );
};

/**
* Append the plan and price by the planId (L-2-P-0)
*
* @param {string} planId The plan id is constructed as 'L-' + payment_plans.parent_level + '-P-' + planId
* @since TBD
* @return {void}
*/
const appendPlanAndPriceByPlanId = ( planId ) => {
const plan = payment_plans.plans.find( plan => plan.id === planId );
appendPlanAndPrice( plan );
};


/**
* Append the plan and price by the plan object
*
* @param {Object} plan The plan object
* @since TBD
* @return {void}
*/
const appendPlanAndPrice = ( plan ) => {
// Bail if no plan is present
if ( ! plan ) {
return;
}
//id="pmpropp_chosen_plan_choice_L-2-P-0"
jQuery( '#pmpropp_chosen_plan_choice_' + plan.id ).prop( 'checked', true );

const data = {
action: 'pmpropp_request_price_change',
pmpro_level: payment_plans.parent_level,
plan: plan.id,
};

jQuery.post( payment_plans.ajaxurl, data, ( response ) => {
if ( response !== '' ) {
localStorage.setItem( 'pmpropp_chosen_plan', plan.id );
jQuery( '#pmpro_level_cost' ).html( response );
}
});
};

0 comments on commit 440458f

Please sign in to comment.