Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save UraraReika/5e944f13d166ec9457698922272197ce to your computer and use it in GitHub Desktop.
Save UraraReika/5e944f13d166ec9457698922272197ce to your computer and use it in GitHub Desktop.
Add new macros to default jet-woo-builder macros list and assign a callback functions.
<?php
add_filter( 'jet-woo-builder/macros/macros-list', function( $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;
}
} 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 );
}
}
return $percentage_sale ? $percentage_sale . '&#37;' : '';
}
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 );
// Handle 0% value.
$percentage_max = round( 100 - ( $prices['sale_min'] / $prices['regular_min'] * 100 ), $precision );
$percentage_sale = $percentage_min ?: $percentage_max;
}
} 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 );
}
}
return $percentage_sale ? $percentage_sale . '&#37;' : '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment