скидка в % для выбранной вариации
Wed Oct 05 2022 15:17:54 GMT+0000 (Coordinated Universal Time)
Saved by
@mastaklance
/**
* Change sale text to percentage
*/
// Always Display the selected variation price for variable products (already working)
add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
if( $variation->get_price() === "" ) return false;
else return true;
}
// Remove the displayed price from variable products in single product pages only
add_action( 'woocommerce_single_product_summary', 'remove_the_displayed_price_from_variable_products', 9 );
function remove_the_displayed_price_from_variable_products() {
global $product;
// Just for variable products
if( ! $product->is_type('variable') ) return;
// Remove the displayed price from variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}
// Display the selected variation discounted price with the discounted percentage for variable products
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
global $product;
// Just for variable products on single product pages
if( $product->is_type('variable') && is_product() ) {
// Getting the clean numeric prices (without html and currency)
$regular_price = floatval( strip_tags($regular_price) );
$sale_price = floatval( strip_tags($sale_price) );
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$percentage_txt = __(' save up to ', 'woocommerce' ).$percentage;
$percentage_txt = __('<p class="ml-sale"> save up to', 'woocommerce' ).$percentage; '</p>';
return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>';
}
return $price;
}
content_copyCOPY
customize .ml-sale class
https://stackoverflow.com/questions/47530715/display-discount-percentage-after-the-selected-variation-sale-price-in-woocommer
Comments