Unverified

Name

Calculated-Many-Selects-PayPal

Language

HTML

Rating

Voted: 0 by 0 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

22/08/2024

Important Note

This snippet has the following status:

Unverified

This snippet has not been verified, use with caution and at your own risk. See details provided by author in sidebar and click below to find out more.

Calculated-Many-Selects-PayPal

 
                    
1<script>
2document.addEventListener('DOMContentLoaded', function() {
3 const optionsQuantityField = document.getElementById('form-field-quantity_options');
4 const servicesQuantityField = document.getElementById('form-field-quantity_services');
5 const optionsPriceField = document.getElementById('form-field-price_options');
6 const servicesPriceField = document.getElementById('form-field-price_services');
7 const totalField = document.getElementById('form-field-total');
8 const optionsField = document.getElementById('form-field-options');
9 const servicesField = document.getElementById('form-field-services');
10 const paypalAmountInput = document.getElementById('paypal-amount');
11 const paypalItemNameInput = document.getElementById('paypal-item-name');
12 
13 // CHANGE: Added this line to select the form element
14 const form = document.getElementById('paypal-form');
15 
16 function getPrice(option) {
17 switch(option) {
18 case 'Option A - $1':
19 return 1;
20 case 'Option B - $1':
21 return 1;
22 case 'Option C - $1':
23 return 1;
24 default:
25 return 0;
26 }
27 }
28 
29 function getServicePrice(service) {
30 switch(service) {
31 case 'Service 1 - $1':
32 return 1;
33 case 'Service 2 - $1':
34 return 1;
35 case 'Service 3 - $1':
36 return 1;
37 default:
38 return 0;
39 }
40 }
41 
42 function updatePrices() {
43 const selectedOption = optionsField.value;
44 const selectedService = servicesField.value;
45 const optionPrice = getPrice(selectedOption);
46 const servicePrice = getServicePrice(selectedService);
47 const optionsQuantity = parseFloat(optionsQuantityField.value) || 0;
48 const servicesQuantity = parseFloat(servicesQuantityField.value) || 0;
49 const optionsTotalPrice = optionsQuantity * optionPrice;
50 const servicesTotalPrice = servicesQuantity * servicePrice;
51 optionsPriceField.value = `$${optionsTotalPrice.toFixed(2)}`;
52 servicesPriceField.value = `$${servicesTotalPrice.toFixed(2)}`;
53 return {
54 selectedOption,
55 optionsQuantity,
56 selectedService,
57 servicesQuantity,
58 optionsTotalPrice,
59 servicesTotalPrice
60 };
61 }
62 
63 function calculateTotal() {
64 const { selectedOption, optionsQuantity, selectedService, servicesQuantity, optionsTotalPrice, servicesTotalPrice } = updatePrices();
65 const total = optionsTotalPrice + servicesTotalPrice;
66 totalField.value = `$${total.toFixed(2)}`;
67 paypalAmountInput.value = total.toFixed(2); // Update PayPal amount
68 paypalItemNameInput.value = `Options: ${optionsQuantity} x ${selectedOption}, Services: ${servicesQuantity} x ${selectedService}`; // Update PayPal item name
69 }
70 
71 // Initial price setting based on default option
72 calculateTotal();
73 
74 // Add event listeners
75 optionsQuantityField.addEventListener('input', calculateTotal);
76 servicesQuantityField.addEventListener('input', calculateTotal);
77 optionsField.addEventListener('change', calculateTotal);
78 servicesField.addEventListener('change', calculateTotal);
79 
80 // CHANGE: Added this block to prevent the form from submitting when the Enter key is pressed
81 form.addEventListener('keypress', function(event) {
82 if (event.key === 'Enter') {
83 event.preventDefault(); // Prevent the form from submitting
84 }
85 });
86});
87</script>

0

Related Snippets

Please see some snippets below related to this snippet..

Elementor

AI Verified

1

PHP: Remove image sizes (controls) from Elementor image dropdowns

Added: 1 year ago

Last Updated: 11 months ago

<p>Since we disable the generation of images besides the original upload, the Elementor controls aren't relevant. This doesn't disable controls on all widgets, unfortunately — but it does remove them...

Elementor

Unverified

0

Elementor-Show-on-Click

Added: 6 months ago

Last Updated: 6 months ago

Show another container when you click on an Icon.

Elementor

AI Verified

0

Elementor Failed Login

Added: 7 months ago

Last Updated: 7 months ago

Return to the current page and add a variable to the URL after a failed login using the Elementor login widget

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

35

Remove Unused Javascript

Added: 2 years ago

Last Updated: 1 week ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 2 years ago

Last Updated: 2 months ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 10 months ago

Last Updated: 3 months ago

This can be used for any WordPress Builder to aid working with CSS Grids.