Skip to content

Instantly share code, notes, and snippets.

@Xiradorn
Created January 6, 2022 19:47
Show Gist options
  • Save Xiradorn/3de4098e35502567fa9e4b736ef716c5 to your computer and use it in GitHub Desktop.
Save Xiradorn/3de4098e35502567fa9e4b736ef716c5 to your computer and use it in GitHub Desktop.
Function for display ogni min (or max) price inside woo shop page / product page
<?php
/**
* Function for display ogni min (or max) price inside woo shop page / product page
*/
function wc_varb_price_range( $wcv_price, $product ) {
$prefix = sprintf('%s: ', __('From', 'wcvp_range'));
$wcv_reg_min_price = $product->get_variation_regular_price( 'min', true );
$wcv_min_sale_price = $product->get_variation_sale_price( 'min', true );
$wcv_max_price = $product->get_variation_price( 'max', true );
$wcv_min_price = $product->get_variation_price( 'min', true );
$wcv_price = ( $wcv_min_sale_price == $wcv_reg_min_price ) ?
wc_price( $wcv_reg_min_price ) :
'<del>' . wc_price( $wcv_reg_min_price ) . '</del>' . '<ins>' . wc_price( $wcv_min_sale_price ) . '</ins>';
return ( $wcv_min_price == $wcv_max_price ) ?
$wcv_price :
sprintf('%s%s', $prefix, $wcv_price);
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_varb_price_range', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_varb_price_range', 10, 2 );
@Simone-De-Santis
Copy link

Simone-De-Santis commented Oct 27, 2022

@Xiradorn
prego aggiunga :

<?php
	add_filter( 'jet-woo-builder/macros/macros-list', '__your_prefix_register_sale_macros', 1, 10 );

	function __your_prefix_register_sale_macros( $macros ) {
		$macros['percentage_sale_max'] = 'get_percentage_sale_max';
		$macros['percentage_sale_min'] = 'get_percentage_sale_min';

		return $macros;
	}
	
	function get_percentage_sale_max() {
		global $product;

		$percentage_sale = '';
		$precision       = 0;

		if ( 'variable' === $product->get_type() ) {

			$prices = jet_woo_builder()->macros->sort_variation_price( $product );

			if ( $prices['regular_min'] > 0 && $prices['regular_max'] > 0 ) {
				$percentage_max  = round( 100 - ( $prices['sale_min'] / $prices['regular_min'] * 100 ), $precision );
				$percentage_sale = $percentage_max . '&#37;';
			}

		} else {

			$regular_price = (float) $product->get_regular_price();
			$sale_price    = (float) $product->get_price();

			if ( $sale_price > 0 && $regular_price > 0 ) {
				$percentage_sale = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '&#37;';
			}

		}

		return 0 < $percentage_sale ? $percentage_sale : '';
	}

	function get_percentage_sale_min() {
		global $product;

		$percentage_sale = '';
		$precision       = 0;

		if ( 'variable' === $product->get_type() ) {

			$prices = jet_woo_builder()->macros->sort_variation_price( $product );

			if ( $prices['regular_min'] > 0 && $prices['regular_max'] > 0 ) {
				$percentage_min  = round( 100 - ( $prices['sale_max'] / $prices['regular_max'] * 100 ), $precision );
				$percentage_sale = $percentage_min . '&#37;';
			}

		} else {

			$regular_price = (float) $product->get_regular_price();
			$sale_price    = (float) $product->get_price();

			if ( $sale_price > 0 && $regular_price > 0 ) {
				$percentage_sale = round( 100 - ( $sale_price / $regular_price * 100 ), $precision ) . '&#37;';
			}

		}

		return 0 < $percentage_sale ? $percentage_sale : '';
	}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment