Last active
April 19, 2024 11:37
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 . '%' : ''; | |
} | |
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 . '%' : ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment