Skip to content

Instantly share code, notes, and snippets.

@ibndawood
Last active Sep 22, 2021
Embed
What would you like to do?
WooCommerce - Show savings in sale flash
<?php
/**
* Get the savings in an onsale product.
*
* @param WC_Product $product The product that is onsale.
* @param string $in Should the savings be absolute or in percentage.
* @return string
*/
function mas_wc_get_savings_on_sale( $product, $in = 'amount' ) {
if ( ! ( $product instanceof WC_Product ) ) {
return '';
}
if ( ! $product->is_on_sale() ) {
return '';
}
if ( $product->is_type( 'variable' ) ) {
$var_regular_price = array();
$var_sale_price = array();
$var_diff_price = array();
$available_variations = $product->get_available_variations();
foreach ( $available_variations as $key => $available_variation ) {
$variation_id = $available_variation['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
$variable_product = new WC_Product_Variation( $variation_id );
$variable_product_regular_price = $variable_product->get_regular_price();
$variable_product_sale_price = $variable_product->get_sale_price();
if ( ! empty( $variable_product_regular_price ) ) {
$var_regular_price[] = $variable_product_regular_price;
} else {
$var_regular_price[] = 0;
}
if ( ! empty( $variable_product_sale_price ) ) {
$var_sale_price[] = $variable_product_sale_price;
} else {
$var_sale_price[] = 0;
}
}
foreach ( $var_regular_price as $key => $reg_price ) {
if ( isset( $var_sale_price[ $key ] ) && 0 !== $var_sale_price[ $key ] ) {
$var_diff_price[] = $reg_price - $var_sale_price[ $key ];
} else {
$var_diff_price[] = 0;
}
}
$best_key = array_search( max( $var_diff_price ), $var_diff_price, true );
$regular_price = $var_regular_price[ $best_key ];
$sale_price = $var_sale_price[ $best_key ];
} else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
$regular_price = wc_get_price_to_display(
$product,
array(
'qty' => 1,
'price' => $regular_price,
)
);
$sale_price = wc_get_price_to_display(
$product,
array(
'qty' => 1,
'price' => $sale_price,
)
);
if ( 'amount' === $in ) {
$savings = wc_price( $regular_price - $sale_price );
} elseif ( 'percentage' === $in ) {
$savings = '<span class="percentage">' . round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ) . '%</span>';
}
return $savings;
}
/**
* Function for overriding the default sale flash with sale amount or percentage.
*/
function mas_wc_get_sale_flash( $html, $post, $product ) {
$savings = mas_wc_get_savings_on_sale( $product, 'percentage' );
if ( ! empty( $savings ) ) {
/* translators: %s - Saved amount or percentage from the sale. */
$html = '<span class="onsale">' . sprintf( esc_html__( 'Save %s', 'your-theme-text-domain' ), $savings ) . '</span>';
}
return apply_filters( 'mas_wc_get_sale_flash', $html, $post, $product );
}
add_filter( 'woocommerce_sale_flash', 'mas_wc_get_sale_flash', 20, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment