• Resolved Big_Rich

    (@big_rich)


    Hello.

    I want to make a calculator for my travel site which will help people to calculate whether a sightseeing pass will save them money.

    So far I’ve modified the “Simple Operations” form by deleting fileds 2, 4 and 6 and then modifying field 8 (adding extra checkboxes and changing the names and values) so that you can select the different attractions which you want to visit using the check boxes and the total price of visiting the selected atractions is displayed in field 9.

    This works well on my localhost test site.

    I now wnat to add a drop down list field at the top of the form which allows you to select between the standard price and the price with a discount card for visiting the chosen attractions.

    Unfortunately this isn’t a simple % calculation since the discounts for each attraction are different.

    Is there some way that I can change the value for each of the check boxes depending on the discount card chosen from a drop down list?

    What I mean is that if in the drop down list I select “no discount” then Item A = $10 , Item B = $5 Item C = $8, Sum of selected Items = $23

    Then if in the drop down list I select “discount card A” then Item A = $8, Item B= $1 Item C= $3, Sum of selected items = $12 (for example)

    Is this possible?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @big_rich,

    If there are only two choices in the DropDown list, the easiest solution would be:

    * Duplicate the checkbox fields, and populate one of them with the standard prices, and the other one with the discount prices (I will call them fieldname1 and fieldname2 respectively, you should use the corresponding names of fields in your form.)

    * Click the link “Show dependencies” in the choices section of the DropDown field, and then select the fieldname1 as the dependent field of the standard choice, and the fieldname2 field as the dependent one to the discount choice.

    * As the values of dependent fields is zero when the fields are disabled, you simply should edit the equation associated to the calculated field as:

    fieldname1+fieldname2

    If one of these fields is disabled the sum won’t be affected.

    Best regards.

    Thread Starter Big_Rich

    (@big_rich)

    Hi Codepeople,

    Firstly thanks for the fast reply, I apreciate it – especialy on a weekend.

    I’ve done as you described and it works.

    The only thing which I’d like to improve is that when you switch between the “discount” and “no discount” lists the check boxes don’t stay selected.

    Can you think of w method whereby the same items would remain selected when I switch between “no discount” and “discount card A”?

    Plugin Author codepeople

    (@codepeople)

    Hello @big_rich,

    They are different fields, so, when standard choice is selected will be displayed the corresponding checkbox field with their choices ticked, and if the discount choice is selected will be displayed the second checkbox field with its own choices.

    If you want synchronize the checkbox fields, an alternative would be, assuming the checkbox fields are the fieldname1 and fieldname2:

    * Insert a “HTML Content” field in the form with the following piece of code as its content:

    
    <script>
    jQuery(document).on('change', '[id*="fieldname1_"]', function(){
    jQuery('[id*="fieldname1_"]').each(function(i){jQuery('[id*="fieldname2_"]:eq('+i+')').prop('checked', this.checked)});
    });
    jQuery(document).on('change', '[id*="fieldname2_"]', function(){
    jQuery('[id*="fieldname2_"]').each(function(i){jQuery('[id*="fieldname1_"]:eq('+i+')').prop('checked', this.checked)});
    });
    </script>
    

    and that’s all.

    Remember to replace the fields’ names in my code with the corresponding ones in your form.

    Best regards.

    Thread Starter Big_Rich

    (@big_rich)

    Thanks @codepeople that works perfectly!

    Plugin Author codepeople

    (@codepeople)

    Hi,

    It has been a pleasure.

    Best regards.

    Thread Starter Big_Rich

    (@big_rich)

    Hi again @codepeople

    I have added a second discount card to the form and syncronized the check boxes using the following code (adding extra lines to the example you gave):

    <script>
    jQuery(document).on('change', '[id*="fieldname1_"]', function(){
    jQuery('[id*="fieldname1_"]').each(function(i){jQuery('[id*="fieldname2_"]:eq('+i+')').prop('checked', this.checked)});
    });
    jQuery(document).on('change', '[id*="fieldname2_"]', function(){
    jQuery('[id*="fieldname2_"]').each(function(i){jQuery('[id*="fieldname1_"]:eq('+i+')').prop('checked', this.checked)});
    });
    jQuery(document).on('change', '[id*="fieldname1_"]', function(){
    jQuery('[id*="fieldname1_"]').each(function(i){jQuery('[id*="fieldname3_"]:eq('+i+')').prop('checked', this.checked)});
    });
    jQuery(document).on('change', '[id*="fieldname2_"]', function(){
    jQuery('[id*="fieldname2_"]').each(function(i){jQuery('[id*="fieldname3_"]:eq('+i+')').prop('checked', this.checked)});
    });
    jQuery(document).on('change', '[id*="fieldname3_"]', function(){
    jQuery('[id*="fieldname3_"]').each(function(i){jQuery('[id*="fieldname1_"]:eq('+i+')').prop('checked', this.checked)});
    });
    jQuery(document).on('change', '[id*="fieldname3_"]', function(){
    jQuery('[id*="fieldname3_"]').each(function(i){jQuery('[id*="fieldname2_"]:eq('+i+')').prop('checked', this.checked)});
    });
    </script>

    This orks but I was wondering if there is a more elegant way of doing this?

    Plugin Author codepeople

    (@codepeople)

    Hello @big_rich,

    The issue is you want synchronize checkbox fields and that requires extra code, of course, maybe there are more elegant solutions, but in principle the execution would be very similar, for example:

    
    <script>
    function synchronize(a,b,swap){
    jQuery(document).on('change', '[id*="'+a+'_"]', function(){
    jQuery('[id*="'+a+'_"]').each(function(i){jQuery('[id*="'+b+'_"]:eq('+i+')').prop('checked', this.checked)});
    });
    if('undefined' == typeof swap) synchronize(b,a,false);
    }
    
    synchronize('fieldname1', 'fieldname2');
    synchronize('fieldname2', 'fieldname3');
    synchronize('fieldname1', 'fieldname3');
    </script>
    

    As you can see, I’ve defined the synchornize function, and I’m calling it passing as parameters the pair of fields’ names, so many times as needed. You don’t need duplicate the logic for every pair of fields.

    Best regards.

    Thread Starter Big_Rich

    (@big_rich)

    Thanks again @codepeople

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Calculate price with & without discount card’ is closed to new replies.