Untitled
unknown
plain_text
5 months ago
3.5 kB
4
Indexable
add_filter('b2bking_tiered_range_display_final', 'add_price_per_unit_display_range', 10, 4); function add_price_per_unit_display_range($price_html, $min_price, $max_price, $product) { // Only run on single product pages if (!is_product()) { return $price_html; } $units_per_product = $product->get_attribute('units'); // set to the actual name of the attribute if (empty($units_per_product) || floatval($units_per_product) === floatval(1)) { return $price_html; } $min_price_per_unit = floatval($min_price) / floatval($units_per_product); $max_price_per_unit = floatval($max_price) / floatval($units_per_product); $formatted_min_price_per_unit = number_format($min_price_per_unit, 2); $formatted_max_price_per_unit = number_format($max_price_per_unit, 2); $new_price_html = $price_html . '<small class="price-per-unit">(' . wc_format_price_range( $formatted_min_price_per_unit, $formatted_max_price_per_unit ). ' per unit)</small>'; return $new_price_html; } add_filter('woocommerce_get_price_html', 'add_price_per_unit_display', 10, 2); function add_price_per_unit_display($price_html, $product) { // Only run on single product pages if (!is_product()) { return $price_html; } $units_per_product = $product->get_attribute('units'); // set to the actual name of the attribute if (empty($units_per_product) || floatval($units_per_product) === floatval(1)) { return $price_html; } if ($product->is_type('variable')){ $min_price = $product->get_variation_price( 'min' ); $max_price = $product->get_variation_price( 'max' ); if ($min_price === $max_price){ $price = $min_price; $price_per_unit = floatval($price) / floatval($units_per_product); $formatted_price_per_unit = number_format($price_per_unit, 2); $new_price_html = $price_html . '<br><small class="price-per-unit">(' . get_woocommerce_currency_symbol() . $formatted_price_per_unit . ' per unit)</small>'; } else { $min_price_per_unit = floatval($min_price) / floatval($units_per_product); $max_price_per_unit = floatval($max_price) / floatval($units_per_product); $formatted_min_price_per_unit = number_format($min_price_per_unit, 2); $formatted_max_price_per_unit = number_format($max_price_per_unit, 2); $new_price_html = $price_html . '<br><small class="price-per-unit">(' . wc_format_price_range( $formatted_min_price_per_unit, $formatted_max_price_per_unit ). ' per unit)</small>'; } } else { if ($product->is_on_sale()) { $price = $product->get_sale_price(); } else { $price = $product->get_regular_price(); } $price_per_unit = floatval($price) / floatval($units_per_product); $formatted_price_per_unit = number_format($price_per_unit, 2); $new_price_html = $price_html . '<br><small class="price-per-unit">(' . get_woocommerce_currency_symbol() . $formatted_price_per_unit . ' per unit)</small>'; } return $new_price_html; } // Add CSS for the price per unit display add_action('wp_head', 'add_price_per_unit_styles'); function add_price_per_unit_styles() { if (is_product()) { ?> <style> .price-per-unit { display: inline-block; font-size: 0.7em; color: #888 !important; } </style> <?php } }
Editor is loading...
Leave a Comment