Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
1.5 kB
3
Indexable
Never
function custom_price_script() {
    $user_id = get_current_user_id();
    $is_b2b = get_user_meta($user_id, 'b2bwhs_b2buser', true);
    if ($is_b2b === 'yes'){
        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_footer', 'custom_price_script');
Leave a Comment