Last active
July 2, 2019 10:43
-
-
Save sholex/4064fc2b656c0857a78228cf5324b370 to your computer and use it in GitHub Desktop.
Woocommerce sale-flash.php for percentages
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 | |
/** | |
* Single Product Sale Flash | |
* | |
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/sale-flash.php. | |
* | |
* HOWEVER, on occasion WooCommerce will need to update template files and you | |
* (the theme developer) will need to copy the new files to your theme to | |
* maintain compatibility. We try to do this as little as possible, but it does | |
* happen. When this occurs the version of the template file will be bumped and | |
* the readme will list any important changes. | |
* | |
* @see https://docs.woocommerce.com/document/template-structure/ | |
* @author WooThemes | |
* @package WooCommerce/Templates | |
* @version 1.6.4 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
global $post, $product; | |
?> | |
<?php if ( $product->is_on_sale() ) : ?> | |
<?php | |
if ($product->is_type( 'simple' )) { | |
$sale_price = $product->get_sale_price(); | |
$regular_price = $product->get_regular_price(); | |
} | |
elseif($product->is_type('variable')){ | |
$sale_price = $product->get_variation_sale_price( 'min', true ); | |
$regular_price = $product->get_variation_regular_price( 'max', true ); | |
} | |
$discount = round (($sale_price / $regular_price -1 ) * 100); | |
?> | |
<?php echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale-icon"> ' . $discount . ' %</span>', $post, $product ); ?> | |
<?php endif; | |
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment