-
Notifications
You must be signed in to change notification settings - Fork 0
/
Showing percentage discount in WooCommerce
75 lines (67 loc) · 2.04 KB
/
Showing percentage discount in WooCommerce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Product loop sale flash
*
* @author Vivek R @ WPSTuffs.com
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
?>
<?php if ($product->is_on_sale() && $product->product_type == 'variable') : ?>
<div class="bubble">
<div class="inside">
<div class="inside-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 callout -->
<?php elseif($product->is_on_sale() && $product->product_type == 'simple') : ?>
<div class="bubble">
<div class="inside">
<div class="inside-text">
<?php
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
echo $price . sprintf( __('%s', 'woocommerce' ), $percentage . '%' ); ?></div>
</div>
</div><!-- end bubble -->
<?php endif; ?>
//-------------------------------
.bubble {
left: 0px;
position: absolute;
text-transform: uppercase;
top: 20px;
z-index: 9;
}
.bubble .inside {
background-color: #e74c3c;
border-radius: 999px;
display: table;
height: 42px;
position: relative;
width: 42px;
-webkit-border-radius: 999px;
}
.bubble .inside .inside-text {
color: #fff;
display: table-cell;
font-size: 14px;
font-weight: bold;
line-height: 14px;
text-align: center;
vertical-align: middle;
}