Created
February 3, 2023 04:51
-
-
Save sarancartrabbit/596b298f770d302aa239ee57a43160c8 to your computer and use it in GitHub Desktop.
Woo Discount v2: Get discount percentage of the product
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
add_filter('advanced_woo_discount_rules_strikeout_price_html', function ($html, $original_price, $discounted_price, $is_variable_product, $initial_price_html, $separator, $original_price_raw, $discounted_price_raw) { | |
if ($original_price != $discounted_price) { | |
if ($initial_price_html) { | |
$html = '<ins>' . $discounted_price . '</ins>' . $separator . '<del>' . $initial_price_html . '</del>'; | |
} else { | |
$html = '<ins>' . $discounted_price . '</ins>' . $separator . '<del>' . $original_price . '</del>'; | |
} | |
$percentage = 0; | |
if(!$is_variable_product){ | |
if($original_price_raw >0) | |
if ($discounted_price_raw < $original_price_raw) { | |
$percentage = round((($original_price_raw - $discounted_price_raw) / $original_price_raw) * 100); | |
} | |
} | |
} | |
if ($is_variable_product) { | |
$space = ' '; | |
$ndash = '–'; | |
$currency_code = '$'; | |
if(function_exists('get_woocommerce_currency_symbol')){ | |
$currency_code = get_woocommerce_currency_symbol(); | |
} | |
$original_prices = explode(" – ", $original_price); | |
$discounted_prices = explode(" – ", $discounted_price); | |
$html = '<ins>' . $discounted_prices[0] . '</ins>' . $space . '<del>' . $original_prices[0] . '</del>'; | |
if(isset($discounted_prices[1])){ | |
$html = $html. $ndash . $space .'<ins>' . $discounted_prices[1] . '</ins>' . $space . '<del>' . $original_prices[1] . '</del>'; | |
} | |
$percentage = 0; | |
$original_price_raw = strip_tags(str_replace($currency_code, '', $original_prices[0])); | |
$discounted_price_raw = strip_tags(str_replace($currency_code, '', $discounted_prices[0])); | |
if($original_price_raw > 0) | |
if ($discounted_price_raw < $original_price_raw) { | |
$percentage = round((($original_price_raw - $discounted_price_raw) / $original_price_raw) * 100); | |
} | |
} | |
if ($percentage > 0) { | |
$html .= " (".$percentage."% off)"; // here you can get the product discount percentage | |
} | |
return $html; | |
}, 10, 8); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment