Jump to content

Change Addon Price on Billing Cycle change


pRieStaKos

Recommended Posts

Hello, 

So I'm trying to apply a addon price override to the `configureproduct.tpl`, using javascript. 

  • When the page loads, the Billing Cycle is Monthly and when I change a configoption, the addon price is OK.
  • If I change Billing Cycle, ex to Annually my js script gets the selected value and returns the addon price as expected.
  • The issue: If I choose another configoption option from the droplist, the script returns nothing.
  • Same if I choose a third option, or the previous selected.
  • If I choose back to Monthly billing cycle, the addon price is wrong again.
  • If I reload the page, all the choosen prices for the selected configoption value are correct.

I admit I'm not a javascript guru, but I've wasted all day with this....

$.each(inputConfigOptionList, function (index, inputConfigOption) {
     if (document.getElementById(inputConfigOption)) {
          if ($('#inputBillingcycle').val() === 'monthly') {
               var str = {$AddonMonthlyPrice};
          } else if ($('#inputBillingcycle').val() === 'quarterly') {
               var str = {$AddonQuarterlyPrice};
          } else if ($('#inputBillingcycle').val() === 'semiannually') {
               var str = {$AddonSemiAnnuallyPrice};
          } else if ($('#inputBillingcycle').val() === 'annually') {
               var str = {$AddonAnnuallyPrice};
          } else if ($('#inputBillingcycle').val() === 'biennially') {
               var str = {$AddonBienniallyPrice};
          } else if ($('#inputBillingcycle').val() === 'triennially') {
               var str = {$AddonTrienniallyPrice};
          }
          var addonPrice = parseFloat(str) + ($('#' + inputConfigOption + ' option:selected').data('price') * 0.20);
     
          $('article:contains("Support Packages")').find('div.panel-price').text('$' + parseFloat(addonPrice).toFixed(2) + ' USD');

          $('#inputBillingcycle').on('change', function () {
               $('article:contains("Support Packages")').find('div.panel-price').text('Calculating...');
               setTimeout(function () {
                    if ($('#inputBillingcycle').val() === 'monthly') {
                         var str = {$AddonMonthlyPrice};
                    } else if ($('#inputBillingcycle').val() === 'quarterly') {
                         var str = {$AddonQuarterlyPrice};
                    } else if ($('#inputBillingcycle').val() === 'semiannually') {
                         var str = {$AddonSemiAnnuallyPrice};
                    } else if ($('#inputBillingcycle').val() === 'annually') {
                         var str = {$AddonAnnuallyPrice};
                    } else if ($('#inputBillingcycle').val() === 'biennially') {
                         var str = {$AddonBienniallyPrice};
                    } else if ($('#inputBillingcycle').val() === 'triennially') {
                         var str = {$AddonTrienniallyPrice};
                    }
                    var addonPrice = parseFloat(str) + ($('#' + inputConfigOption + ' option:selected').data('price') * 0.20);
                    $('article:contains("Support Packages")').find('div.panel-price').text('$' + parseFloat(addonPrice).toFixed(2) + ' USD');
               }, 3000);
          });

          $('#' + inputConfigOption).change(function() {
               var str = addonPrice = 0;
               $('article:contains("Support Packages")').find('div.panel-price').text('Calculating...');
               setTimeout(function () {
                    if ($('#inputBillingcycle').val() === 'monthly') {
                         var str = {$AddonMonthlyPrice};
                    } else if ($('#inputBillingcycle').val() === 'quarterly') {
                         var str = {$AddonQuarterlyPrice};
                    } else if ($('#inputBillingcycle').val() === 'semiannually') {
                         var str = {$AddonSemiAnnuallyPrice};
                    } else if ($('#inputBillingcycle').val() === 'annually') {
                         var str = {$AddonAnnuallyPrice};
                    } else if ($('#inputBillingcycle').val() === 'biennially') {
                         var str = {$AddonBienniallyPrice};
                    } else if ($('#inputBillingcycle').val() === 'triennially') {
                         var str = {$AddonTrienniallyPrice};
                    }
                    var addonPrice = parseFloat(str) + ($('#' + inputConfigOption + ' option:selected').data('price') * 0.20);  
                    $('article:contains("Support Packages")').find('div.panel-price').text('$' + parseFloat(addonPrice).toFixed(2) + ' USD');
               }, 500);
          })
     }
});

Any help ? Please dont' ask why I want to override the addon price when I choose a specific configoption. It's a task with predefined request 😄

Thank you in advance.

PS: I came across this old bug from 2018.  Hilarious....

 

Edited by pRieStaKos
Link to comment
Share on other sites

I believe the "option:selected" you have is messing it up as that will find the element for the option that is by default selected and not the the currently selected option.   Instead I think that should be:

var addonPrice = parseFloat(str) + ($('#' + inputConfigOption).val() * 0.20);  

 

Link to comment
Share on other sites

7 hours ago, steven99 said:

I believe the "option:selected" you have is messing it up as that will find the element for the option that is by default selected and not the the currently selected option.   Instead I think that should be:


var addonPrice = parseFloat(str) + ($('#' + inputConfigOption).val() * 0.20);  

 

Hello @steven99

Thank you for your response.

I want to get the `data('price')` of the selected option. Not the selected value.

Edited by pRieStaKos
Link to comment
Share on other sites

So, after a coffee and with clear mind, I managed to resolve this by adding a onchange function on select.

function getPrice(obj) {
     var uid = obj.options[obj.selectedIndex].getAttribute('data-price');
     var str = addonPrice = 0;
     $('article:contains("Support Packages")').find('div.panel-price').text('Calculating...');
     setTimeout(function () {
          if ($('#inputBillingcycle').val() === 'monthly') {
               var str = {$AddonMonthlyPrice};
          } else if ($('#inputBillingcycle').val() === 'quarterly') {
               var str = {$AddonQuarterlyPrice};
          } else if ($('#inputBillingcycle').val() === 'semiannually') {
               var str = {$AddonSemiAnnuallyPrice};
          } else if ($('#inputBillingcycle').val() === 'annually') {
               var str = {$AddonAnnuallyPrice};
          } else if ($('#inputBillingcycle').val() === 'biennially') {
               var str = {$AddonBienniallyPrice};
          } else if ($('#inputBillingcycle').val() === 'triennially') {
               var str = {$AddonTrienniallyPrice};
          }
          var addonPrice = parseFloat(str) + (obj.options[obj.selectedIndex].getAttribute('data-price') * 0.20);
          $('article:contains("Support Packages")').find('div.panel-price').text('$' + parseFloat(addonPrice).toFixed(2) + ' USD');
     }, 500);
}

Thank you anyway.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated