Untitled
webwizards
plain_text
3 months ago
2.8 kB
13
Indexable
add_action('wp_head', 'add_unit_price_updater_script');
function add_unit_price_updater_script() {
?>
<script type="text/javascript">
// Monitor subtotal changes and update unit price
jQuery(document).ready(function() {
// Function to update unit price based on subtotal and quantity
function updateUnitPrice(containerElement) {
var subtotalElement = containerElement.find('.b2bking_bulkorder_form_container_content_line_subtotal_cream');
var quantityElement = containerElement.find('.b2bking_bulkorder_form_container_content_line_qty_cream');
var priceElement = containerElement.find('.b2bking_bulkorder_cream_sku .woocommerce-Price-amount');
if (subtotalElement.length && quantityElement.length && priceElement.length) {
// Get subtotal value from data-value attribute
var subtotalValue = parseFloat(subtotalElement.attr('data-value'));
// Get quantity value
var quantityValue = parseInt(quantityElement.val()) || 1;
// Calculate unit price
var unitPrice = subtotalValue / quantityValue;
// Update the price display
priceElement.html('<bdi><span class="woocommerce-Price-currencySymbol">$</span>' + unitPrice.toFixed(2) + '</bdi>');
}
}
// Use MutationObserver to watch for changes in subtotal elements
function observeSubtotalChanges() {
var productContainers = jQuery('.b2bking_bulkorder_form_container_content_line_cream');
productContainers.each(function() {
var container = jQuery(this);
var subtotalElement = container.find('.b2bking_bulkorder_form_container_content_line_subtotal_cream')[0];
if (subtotalElement) {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'attributes' && mutation.attributeName === 'data-value') {
updateUnitPrice(container);
}
});
});
observer.observe(subtotalElement, {
attributes: true,
attributeFilter: ['data-value']
});
// Initial calculation
updateUnitPrice(container);
}
});
}
// Initialize observers
observeSubtotalChanges();
});
</script>
<?php
}Editor is loading...
Leave a Comment