I have a monthly subscription based WordPress website, which gives users monthly content and automatically moves them to the next subscription at the end of each month (but requires payment to proceed). Users have a page which lists all the subscription levels (and which ones they've purchased) on the website. I am using a plugin called Ultimate Membership Pro to accomplish this.
The problem comes in when we have had to integrate it with WooCommerce (for good reason). Because of this, when a user has purchased a subscription level it still gives the option to purchase it again on the subscription levels page.
I disabled the repeat purchase functionality with php, but I need to the purchase button to be disabled dynamically on the subscription list page (an example of which is listed in the fiddles below).
Using a foreach
loop and some DOM tree traversing, jQuery finds the name of the level where it is listed at the top and disables its button on the page. This works just fine.
The problem is that it doesn't work when there is more than one level listed.
I have two fiddles which demonstrate the issue.
PS because I am working in Wordpress my jQuery is written in no-conflict mode.
jQuery(function($) {
$('.ihc-level-item .ihc-level-item-title').each(function() {
// Get the level name
var lvlName = $('.ihc-account-subscr-list .ihc-level-name').text();
// Find that level on page
if ($(this).html() == lvlName) {
// Console log confirms its working
console.log('Found level: ' + lvlName + ' on page.');
$(this).closest('.ihc-level-item').children('.ihc-level-item-content').addClass('lvl-purchased');
}
});
});
Working example
Only one level listed
Non working example
Two levels listed
I solved it using meagar♦'s answer here
I just needed to store the level names in an array and access it and such then presto!
$('.ihc-level-item .ihc-level-item-title').each(function() {
// Get the level names and store inside array
var array = [];
var lvlName = $('.ihc-account-subscr-list .ihc-level-name').map(function () { return $(this).text(); }).get();
console.log(lvlName);
for (var i=0; i<lvlName.length; i++) {
// Find that level on page
if ($(this).html() == lvlName[i]) {
$(this).closest('.ihc-level-item').children('.ihc-level-item-content').addClass('lvl-purchased');
} else {
console.log('Couldn\'t find level on page.');
}
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments