|
1 | 1 | <?php |
2 | 2 | // WooCommerce - Display product currency and price inside "Add to Cart" button |
3 | | -// Last update: 2024-09-17 |
| 3 | +// Last update: 2025-10-08 |
4 | 4 |
|
5 | 5 | if (class_exists('WooCommerce') && WC()) { |
6 | 6 |
|
7 | | - add_filter($hook_name = 'woocommerce_product_single_add_to_cart_text', $callback = 'woocommerce_add_to_cart_product_price', $priority = 10, $accepted_args = 2); |
| 7 | + add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_add_to_cart_product_price', 10, 2); |
8 | 8 |
|
9 | 9 | function woocommerce_add_to_cart_product_price($button_text, $product) |
10 | 10 | { |
11 | | - if (is_product()) { |
12 | | - // Get product price |
13 | | - $product_price = wc_get_price_to_display($product); |
14 | | - |
15 | | - // Check if the product is a variable product |
16 | | - if ($product->is_type('variable')) { |
17 | | - // Get variations data |
18 | | - $variations_data = array_column($product->get_available_variations(), 'display_price', 'variation_id'); |
19 | | - ?> |
20 | | - <script> |
21 | | - jQuery(function($) { |
22 | | - // JSON data for variations |
23 | | - const jsonData = <?php echo json_encode($variations_data); ?>; |
24 | | - const inputVID = 'input.variation_id'; |
25 | | - const quantityInput = 'input[name="quantity"]'; |
26 | | - const currencySymbol = '<?php echo esc_html(get_woocommerce_currency_symbol()); ?>'; |
27 | | - const currencyPosition = '<?php echo esc_html(get_option("woocommerce_currency_pos")); ?>'; |
28 | | - const currencyDecimals = '<?php echo esc_html(wc_get_price_decimals()); ?>'; |
29 | | - const locale = 'de-DE'; |
30 | | - |
31 | | - // Number formatter for price |
32 | | - const formatter = new Intl.NumberFormat(locale, { |
33 | | - minimumFractionDigits: parseInt(currencyDecimals), |
34 | | - maximumFractionDigits: parseInt(currencyDecimals) |
35 | | - }); |
36 | | - |
37 | | - // Function to format price based on currency position |
38 | | - function formatPrice(price) { |
39 | | - let formattedPrice = formatter.format(price); |
40 | | - switch(currencyPosition) { |
41 | | - case 'left': |
42 | | - return currencySymbol + formattedPrice; |
43 | | - case 'right': |
44 | | - return formattedPrice + currencySymbol; |
45 | | - case 'left_space': |
46 | | - return currencySymbol + ' ' + formattedPrice; |
47 | | - case 'right_space': |
48 | | - return formattedPrice + ' ' + currencySymbol; |
49 | | - default: |
50 | | - return currencySymbol + formattedPrice; // default to left if unknown position |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - // Function to update price |
55 | | - function updatePrice() { |
56 | | - const vid = $(inputVID).val(); |
57 | | - const quantity = parseInt($(quantityInput).val()) || 1; |
58 | | - if (vid && jsonData[vid] !== undefined) { |
59 | | - const price = jsonData[vid] * quantity; |
60 | | - const priceHtml = formatPrice(price); |
61 | | - $("button.single_add_to_cart_button span[data-price='true']").remove(); |
62 | | - $(".single_add_to_cart_button").append("<span data-price='true'> - " + priceHtml + "</span>"); // Append price to button |
63 | | - } else { |
64 | | - $("button.single_add_to_cart_button span[data-price='true']").remove(); // Remove existing price if variation is not selected |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - // Initial price update |
69 | | - updatePrice(); |
70 | | - // Event listeners for variation ID and quantity changes |
71 | | - $(inputVID + ', ' + quantityInput).on('change', updatePrice); |
72 | | - // Event listener for quantity change using buttons |
73 | | - $('button.plus, button.minus').on('click', function() { |
74 | | - setTimeout(updatePrice, 0); |
75 | | - }); |
76 | | - }); |
77 | | - </script> |
78 | | - <?php |
79 | | - } else { |
80 | | - // For single products, display regular product price |
81 | | - ?> |
82 | | - <script> |
83 | | - jQuery(function($) { |
84 | | - const quantityInput = 'input[name="quantity"]'; |
85 | | - const currencySymbol = '<?php echo esc_html(get_woocommerce_currency_symbol()); ?>'; |
86 | | - const currencyPosition = '<?php echo esc_html(get_option("woocommerce_currency_pos")); ?>'; |
87 | | - const currencyDecimals = '<?php echo esc_html(wc_get_price_decimals()); ?>'; |
88 | | - const basePrice = <?php echo $product_price; ?>; |
89 | | - const locale = 'de-DE'; |
90 | | - |
91 | | - // Number formatter for price |
92 | | - const formatter = new Intl.NumberFormat(locale, { |
93 | | - minimumFractionDigits: parseInt(currencyDecimals), |
94 | | - maximumFractionDigits: parseInt(currencyDecimals) |
95 | | - }); |
| 11 | + if (!is_product()) { |
| 12 | + return $button_text; |
| 13 | + } |
96 | 14 |
|
97 | | - // Function to format price based on currency position |
98 | | - function formatPrice(price) { |
99 | | - let formattedPrice = formatter.format(price); |
100 | | - switch(currencyPosition) { |
101 | | - case 'left': |
102 | | - return currencySymbol + formattedPrice; |
103 | | - case 'right': |
104 | | - return formattedPrice + currencySymbol; |
105 | | - case 'left_space': |
106 | | - return currencySymbol + ' ' + formattedPrice; |
107 | | - case 'right_space': |
108 | | - return formattedPrice + ' ' + currencySymbol; |
109 | | - default: |
110 | | - return currencySymbol + formattedPrice; // default to left if unknown position |
111 | | - } |
112 | | - } |
| 15 | + // Common configuration for both product types |
| 16 | + $currency_config = sprintf( |
| 17 | + 'const currencySymbol = %s; |
| 18 | + const currencyPosition = %s; |
| 19 | + const currencyDecimals = %s; |
| 20 | + const locale = "de-DE"; |
| 21 | + |
| 22 | + const formatter = new Intl.NumberFormat(locale, { |
| 23 | + minimumFractionDigits: parseInt(currencyDecimals), |
| 24 | + maximumFractionDigits: parseInt(currencyDecimals) |
| 25 | + }); |
| 26 | + |
| 27 | + function formatPrice(price) { |
| 28 | + const formattedPrice = formatter.format(price); |
| 29 | + switch(currencyPosition) { |
| 30 | + case "left": return currencySymbol + formattedPrice; |
| 31 | + case "right": return formattedPrice + currencySymbol; |
| 32 | + case "left_space": return currencySymbol + " " + formattedPrice; |
| 33 | + case "right_space": return formattedPrice + " " + currencySymbol; |
| 34 | + default: return currencySymbol + formattedPrice; |
| 35 | + } |
| 36 | + }', |
| 37 | + wp_json_encode(get_woocommerce_currency_symbol()), |
| 38 | + wp_json_encode(get_option('woocommerce_currency_pos')), |
| 39 | + wp_json_encode(wc_get_price_decimals()) |
| 40 | + ); |
113 | 41 |
|
114 | | - // Function to update price |
115 | | - function updatePrice() { |
116 | | - const quantity = parseInt($(quantityInput).val()) || 1; |
117 | | - const price = basePrice * quantity; |
118 | | - const priceHtml = formatPrice(price); |
119 | | - $("button.single_add_to_cart_button span[data-price='true']").remove(); |
120 | | - $(".single_add_to_cart_button").append("<span data-price='true'> - " + priceHtml + "</span>"); // Append price to button |
| 42 | + if ($product->is_type('variable')) { |
| 43 | + $variations_data = array_column($product->get_available_variations(), 'display_price', 'variation_id'); |
| 44 | + ?> |
| 45 | + <script> |
| 46 | + jQuery(function($) { |
| 47 | + const jsonData = <?php echo wp_json_encode($variations_data); ?>; |
| 48 | + const $button = $(".single_add_to_cart_button"); |
| 49 | + const $varInput = $("input.variation_id"); |
| 50 | + const $qtyInput = $('input[name="quantity"]'); |
| 51 | + |
| 52 | + <?php echo $currency_config; ?> |
| 53 | + |
| 54 | + function updatePrice() { |
| 55 | + const vid = $varInput.val(); |
| 56 | + const quantity = parseInt($qtyInput.val()) || 1; |
| 57 | + |
| 58 | + $button.find('span[data-price]').remove(); |
| 59 | + |
| 60 | + if (vid && jsonData[vid] !== undefined) { |
| 61 | + const priceHtml = formatPrice(jsonData[vid] * quantity); |
| 62 | + $button.append('<span data-price> - ' + priceHtml + '</span>'); |
121 | 63 | } |
122 | | - |
123 | | - // Initial price update |
124 | | - updatePrice(); |
125 | | - // Event listeners for quantity changes |
126 | | - $(quantityInput).on('change', updatePrice); |
127 | | - // Event listener for quantity change using buttons |
128 | | - $('button.plus, button.minus').on('click', function() { |
129 | | - setTimeout(updatePrice, 0); |
130 | | - }); |
131 | | - }); |
132 | | - </script> |
133 | | - <?php |
134 | | - } |
135 | | - |
136 | | - return $button_text; |
| 64 | + } |
| 65 | + |
| 66 | + updatePrice(); |
| 67 | + $varInput.add($qtyInput).on('change', updatePrice); |
| 68 | + $('button.plus, button.minus').on('click', () => setTimeout(updatePrice, 0)); |
| 69 | + }); |
| 70 | + </script> |
| 71 | + <?php |
| 72 | + } else { |
| 73 | + $product_price = wc_get_price_to_display($product); |
| 74 | + ?> |
| 75 | + <script> |
| 76 | + jQuery(function($) { |
| 77 | + const basePrice = <?php echo $product_price; ?>; |
| 78 | + const $button = $(".single_add_to_cart_button"); |
| 79 | + const $qtyInput = $('input[name="quantity"]'); |
| 80 | + |
| 81 | + <?php echo $currency_config; ?> |
| 82 | + |
| 83 | + function updatePrice() { |
| 84 | + const quantity = parseInt($qtyInput.val()) || 1; |
| 85 | + const priceHtml = formatPrice(basePrice * quantity); |
| 86 | + |
| 87 | + $button.find('span[data-price]').remove(); |
| 88 | + $button.append('<span data-price> - ' + priceHtml + '</span>'); |
| 89 | + } |
| 90 | + |
| 91 | + updatePrice(); |
| 92 | + $qtyInput.on('change', updatePrice); |
| 93 | + $('button.plus, button.minus').on('click', () => setTimeout(updatePrice, 0)); |
| 94 | + }); |
| 95 | + </script> |
| 96 | + <?php |
137 | 97 | } |
138 | 98 |
|
139 | 99 | return $button_text; |
140 | 100 | } |
141 | | - |
142 | 101 | } |
0 commit comments