Advertisement
shuvocse

Display the discounted price and percentage on Woocommerce p

Nov 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
  2. function change_displayed_sale_price_html( $price, $product ) {
  3. // Only on sale products on frontend and excluding min/max price on variable products
  4. if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
  5. // Get product prices
  6. $regular_price = (float) $product->get_regular_price(); // Regular price
  7. $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)
  8.  
  9. // "Saving Percentage" calculation and formatting
  10. $precision = 1; // Max number of decimals
  11. $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';
  12.  
  13. // Append to the formated html price
  14. $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
  15. }
  16. return $price;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement