Skip to content

Instantly share code, notes, and snippets.

@eltondev
Last active August 29, 2015 14:19
Show Gist options
  • Save eltondev/0c84c9afe1d6b4976011 to your computer and use it in GitHub Desktop.
Save eltondev/0c84c9afe1d6b4976011 to your computer and use it in GitHub Desktop.
Display Percentage
.percentage-bubble {
left: 0px;
position: absolute;
text-transform: uppercase;
top: 20px;
z-index: 9;
}
.percentage-bubble .percentage {
background-color: #e74c3c;
border-radius: 999px;
display: table;
height: 42px;
position: relative;
width: 42px;
-webkit-border-radius: 999px;
}
.percentage-bubble .percentage-inside .percentage-text {
color: #fff;
display: table-cell;
font-size: 14px;
font-weight: bold;
line-height: 14px;
text-align: center;
vertical-align: middle;
}
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
?>
<?php if ($product->is_on_sale() && $product->product_type == 'variable') : ?>
<div class="percentage-bubble">
<div class="percentage">
<div class="percentage-text">
<?php
$available_variations = $product->get_available_variations();
$maximumper = 0;
for ($i = 0; $i < count($available_variations); ++$i) {
$variation_id=$available_variations[$i]['variation_id'];
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
$percentage= round((( ( $regular_price - $sales_price ) / $regular_price ) * 100),1) ;
if ($percentage > $maximumper) {
$maximumper = $percentage;
}
}
echo $price . sprintf( __('%s', 'woocommerce' ), $maximumper . '%' ); ?></div>
</div>
</div><!-- end -->
<?php elseif($product->is_on_sale() && $product->product_type == 'simple') : ?>
<div class="percentage-bubble">
<div class="percentage">
<div class="percentage-text">
<?php
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
echo $price . sprintf( __('%s', 'woocommerce' ), $percentage . '%' ); ?></div>
</div>
</div><!-- end ->
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment