Untitled
unknown
plain_text
a year ago
1.5 kB
14
Indexable
function enqueue_custom_price_script() {
if (is_product()) {
wp_enqueue_script('custom-price-script', get_template_directory_uri() . '/js/custom-price-script.js', array('jquery'), '1.0', true);
}
}
add_action('wp_enqueue_scripts', 'enqueue_custom_price_script');
function custom_price_script() {
if (is_product()) {
?>
<script>
jQuery(document).ready(function(jQuery) {
// Extract sq ft number
var sqFtElement = jQuery('tr.woocommerce-product-attributes-item--attribute_sq-ftbox td.woocommerce-product-attributes-item__value p');
if (sqFtElement.length) {
var sqFt = parseFloat(sqFtElement.text());
if (!isNaN(sqFt) && sqFt > 0) {
// Function to calculate and update price
function updatePrice(priceElement) {
var originalPrice = parseFloat(priceElement.text().replace(/[^0-9.-]+/g, ""));
if (!isNaN(originalPrice)) {
var pricePerSqFt = (originalPrice / sqFt).toFixed(2);
priceElement.text('$' + pricePerSqFt + ' per sq ft');
}
}
// Update prices within .first.price class only
jQuery('.first.price .woocommerce-Price-amount').each(function() {
updatePrice(jQuery(this));
});
}
}
});
</script>
<?php
}
}
add_action('wp_head', 'custom_price_script');Editor is loading...
Leave a Comment