Skip to content

Instantly share code, notes, and snippets.

@eltondev
Last active August 29, 2015 14:19
Show Gist options
  • Save eltondev/13046a0ed16e9362c057 to your computer and use it in GitHub Desktop.
Save eltondev/13046a0ed16e9362c057 to your computer and use it in GitHub Desktop.
Sales Flash Gradient
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
if ( ! $product->is_in_stock() ) return;
$sale_price = get_post_meta( $product->id, '_price', true);
$regular_price = get_post_meta( $product->id, '_regular_price', true);
if (empty($regular_price)){
$available_variations = $product->get_available_variations();
$variation_id=$available_variations[0]['variation_id'];
$variation= new WC_Product_Variation( $variation_id );
$regular_price = $variation ->regular_price;
$sale_price = $variation ->sale_price;
}
$sale = ceil(( ($regular_price - $sale_price) / $regular_price ) * 100);
?>
<?php if ( !empty( $regular_price ) && !empty( $sale_price ) && $regular_price > $sale_price ) : ?>
<?php
$R=floor((255*$sale)/100);
$G=floor((255*(100-$sale))/100);
$bg_style = 'background:none;background-color: rgb(' . $R . ',' . $G . ',0);';
?>
<?php echo
apply_filters( 'woocommerce_sale_flash', '<span class="onsale" style="'. $bg_style .'">-' . $sale . '%</span>', $post, $product );
?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment