Untitled

 avatar
webwizards
plain_text
15 days ago
4.5 kB
4
No Index
add_action('woocommerce_after_add_to_cart_button', function(){
	echo do_shortcode('[b2bking_quote_form type=button]');
	?>
	<style>.single_add_to_cart_button{display:none !important}</style>
	<?php
});
add_action('wp_head', function(){
	?>
	<script>
		jQuery(document).ready(function() {
			jQuery('body').on('click', '.b2bking_send_custom_quote_button', function() {
				var selectedOptions = [];
				// 1. Get the selected bear option
				var bearOption = jQuery('#pa_bear-options').val();
				var bearOptionText = jQuery('#pa_bear-options option:selected').text();
				if (bearOption && bearOption !== '') {
					selectedOptions.push('Option: ' + bearOptionText);
				}
				// 2. Get the quantity and price calculation
				var quantity = jQuery('input[name="quantity"]').val() || jQuery('.qty').val();
				if (quantity) {
					// Look for the price in td with b2bking_has_color class
					var priceElement = jQuery('td.b2bking_has_color .woocommerce-Price-amount');
					var unitPrice = null;
					
					if (priceElement.length > 0) {
						// Extract the price value from the price element
						var priceText = priceElement.text().replace(/[^\d.,]/g, ''); // Remove currency symbols and other non-numeric chars
						unitPrice = parseFloat(priceText);
					}
					
					// If we couldn't find it in the td, try the hidden input as fallback
					if (!unitPrice || isNaN(unitPrice)) {
						var hiddenPriceInput = jQuery('td.b2bking_has_color .b2bking_hidden_tier_value');
						if (hiddenPriceInput.length > 0) {
							unitPrice = parseFloat(hiddenPriceInput.val());
						}
					}
					
					// Format quantity display with price calculation if available
					if (unitPrice && !isNaN(unitPrice)) {
						var total = (parseFloat(quantity) * unitPrice).toFixed(2);
						selectedOptions.push('Quantity: ' + quantity + ' @ ' + unitPrice.toFixed(2) + ' = ' + total);
					} else {
						selectedOptions.push('Quantity: ' + quantity);
					}
				}
				// 3. Find all checked radio buttons and checkboxes within ppom-rendering-fields
				jQuery('.ppom-rendering-fields input:checked').each(function() {
					var input = jQuery(this);
					var fieldTitle = input.data('title') || '';
					var optionLabel = input.data('label') || input.val() || '';
					var price = input.data('price') || '';
					// Format the option text
					var optionText = '';
					if (fieldTitle) {
						optionText += fieldTitle + ': ';
					}
					optionText += optionLabel;
					if (price) {
						optionText += ' [+£' + price + ']';
					}
					selectedOptions.push(optionText);
				});
				// Calculate total if we have a base total from quantity calculation
				var grandTotal = 0;
				var hasBaseTotal = false;
				
				// Look for the base total from quantity calculation
				selectedOptions.forEach(function(option) {
					if (option.indexOf('Quantity:') === 0 && option.indexOf(' = ') > -1) {
						var equalIndex = option.lastIndexOf(' = ');
						if (equalIndex > -1) {
							var baseTotal = parseFloat(option.substring(equalIndex + 3));
							if (!isNaN(baseTotal)) {
								grandTotal += baseTotal;
								hasBaseTotal = true;
							}
						}
					}
				});
				
				// Add any addon costs (format: [+£XX.XX])
				selectedOptions.forEach(function(option) {
					var matches = option.match(/\[\+£(\d+(?:\.\d{2})?)\]/g);
					if (matches) {
						matches.forEach(function(match) {
							var price = parseFloat(match.replace(/\[\+£|\]/g, ''));
							if (!isNaN(price)) {
								grandTotal += price;
							}
						});
					}
				});
				
				// Join all selected options with line breaks
				var formattedText = selectedOptions.join('\n');
				
				// Add total line if we have calculations
				if (hasBaseTotal || grandTotal > 0) {
					formattedText += '\nTotal: ' + grandTotal.toFixed(2);
				}
				
				// Get current textarea content
				var currentContent = jQuery('#b2bking_request_custom_quote_textarea').val();
				// Add the formatted text to the end of the textarea
				// Add a line break if there's already content
				var newContent = currentContent + (currentContent ? '\n\n' : '') + formattedText;
				// Update the textarea
				jQuery('#b2bking_request_custom_quote_textarea').val(newContent);
				jQuery('#b2bking_request_custom_quote_textarea').show();
				jQuery('#b2bking_request_custom_quote_textarea').focus();
				var textarea = jQuery('#b2bking_request_custom_quote_textarea')[0];
				textarea.scrollTop = textarea.scrollHeight;
			});
		});
	</script>
	<?php
});
Editor is loading...
Leave a Comment