-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-dynamic-pricing-display-table.js
115 lines (97 loc) · 3.95 KB
/
woocommerce-dynamic-pricing-display-table.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var WooCommerceDynamicPricingDisplayTable = function() {
var dt = this;
dt.variation = null;
dt.prices = [];
dt.init = function() {
;(function ( $, window, document, undefined ) {
$variation_form = $( '.variations_form' );
$variation_form.on( 'show_variation', function( event, variation ) {
dt.updatePricingTable(variation);
} );
$quantity_field = $( '.input-text.qty' );
$quantity_field.on( 'input', dt.updateDisplayPrice );
})( jQuery, window, document );
};
dt.updateDisplayPrice = function() {
var qty = parseInt( $quantity_field[0].value );
if(qty) {
for( var key in dt.prices ) {
if( qty >= dt.prices[key].from && qty <= dt.prices[key].to ) {
var discounted_price = dt.prices[key].price;
} else if( qty >= dt.prices[key].from && dt.prices[key].to === null ) {
var discounted_price = dt.prices[key].price;
}
}
if(discounted_price) {
jQuery( 'ins .woocommerce-Price-amount.amount' ).html( '<span class="woocommerce-Price-currencySymbol">$</span>'+ discounted_price +' each');
}
}
};
dt.updatePricingTable = function( variation ) {
dt.variation = variation;
var pricing = wcdpdt_wc_product_pricing;
var target = document.querySelector( 'p.price' );
target.innerHTML = '';
var element = document.getElementById( 'pricebreaks' );
var appendElement = false;
if(!element) {
element = document.createElement( 'div' );
element.id = 'pricebreaks';
element.className = 'pricebreaks';
appendElement = true;
}
dt.prices.push( { 'from': 1, 'to': 1, 'price': variation.display_price.toFixed( wcdpdt_wc_price_num_decimals ) } );
var html = '<div class="pricebreak"><div class="heading">Qty</div><div class="heading">Price</div><div class="heading">Sale</div></div></div>';
html += '<div class="pricebreak"><div class="qty">1</div><div class="price">$'+ variation.display_regular_price.toFixed( wcdpdt_wc_price_num_decimals ) +'</div><div class="sale_price">$'+ variation.display_price.toFixed( wcdpdt_wc_price_num_decimals ) +'</div></div>';
if(pricing.rules) {
for( var key in pricing.rules[0] ) {
var rule = pricing.rules[0][key];
var from = rule.from ? parseInt( rule.from ) : null;
var to = rule.to ? parseInt( rule.to ) : null;
var amount = rule.amount ? parseFloat( rule.amount ) : null;
var price = 0;
switch(rule.type) {
case 'percentage_discount':
price = dt.ceil(variation.display_price - ( variation.display_price * ( amount / 100 ) ), -2 );
sale_price = dt.ceil(variation.display_regular_price - ( variation.display_regular_price * ( amount / 100 ) ), -2 );
break;
case 'price_discount':
price = ( variation.display_price - amount );
sale_price = ( variation.display_regular_price - amount );
break;
case 'fixed_price':
price = amount;
sale_price = amount;
break;
}
price = price.toFixed( wcdpdt_wc_price_num_decimals );
sale_price = sale_price.toFixed( wcdpdt_wc_price_num_decimals );
if(price > 0 && rule.from > 0) {
html += '<div class="pricebreak"><div class="qty">'+ rule.from +'</div><div class="price">$'+ sale_price +'</div><div class="sale_price">$'+ price +'</div></div>';
dt.prices.push( { 'from': from, 'to': to, 'price': price, 'sale_price': sale_price } );
}
}
element.innerHTML = html;
if( appendElement ) {
target.appendChild( element );
}
dt.updateDisplayPrice();
}
};
dt.ceil = function( value, exp ) {
if ( typeof exp === 'undefined' || +exp === 0 ) {
return Math['ceil']( value );
}
value = +value;
exp = +exp;
if (isNaN( value ) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
value = value.toString().split('e');
value = Math['ceil']( + ( value[0] + 'e' + ( value[1] ? ( +value[1] - exp ) : - exp ) ) );
value = value.toString().split( 'e' );
return + ( value[0] + 'e' + ( value[1] ? ( + value[1] + exp ) : exp ) );
};
dt.init();
}
new WooCommerceDynamicPricingDisplayTable();