wc-price.js
3.8 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
( function ( $ ) {
"use strict";
var WC_Price = function (options) {
this.init( 'wc_price', options, WC_Price.defaults );
};
$.fn.editableutils.inherit( WC_Price, $.fn.editabletypes.abstractinput );
$.extend( WC_Price.prototype, {
render: function() {
var container = this.$input;
container.find( '.toggle-price-schedule' ).click( function( e ) {
e.preventDefault();
var el = container.find( '.price-schedule' );
if ( el.is( ':visible' ) ) {
el.hide();
$( this ).text( 'Schedule' );
container.find( '.price-schedule input' ).val( '' );
}
else {
el.show();
$( this ).text( 'Cancel' );
}
} );
var dates = $( '.price-schedule input' ).datepicker( {
defaultDate: '',
dateFormat: 'yy-mm-dd',
numberOfMonths: 1,
showButtonPanel: true,
showOn: 'button',
buttonImage: woocommerce_admin_meta_boxes.calendar_image,
buttonImageOnly: true,
onSelect: function( selectedDate ) {
var option = $( this ).is( '[name="sale_price_dates_from"]') ? 'minDate' : 'maxDate';
var instance = $( this ).data( 'datepicker' );
var date = $.datepicker.parseDate(
instance.settings.dateFormat || $.datepicker._defaults.dateFormat,
selectedDate,
instance.settings
);
dates.not( this ).datepicker( 'option', option, date );
}
} );
},
activate: function() {
var value = this.input2value();
this.$input.find( 'input:first' ).focus();
if ( value.sale_price_dates_from || value.sale_price_dates_to ) {
this.$input.find( '.toggle-price-schedule' ).trigger( 'click' );
}
},
value2input: function( value ) {
if ( ! value ) {
return;
}
this.$input.find( '[name="regular_price"]' ).val( value.regular_price );
this.$input.find( '[name="sale_price"]' ).val( value.sale_price );
this.$input.find( '[name="sale_price_dates_from"]' ).val( value.sale_price_dates_from );
this.$input.find( '[name="sale_price_dates_to"]' ).val( value.sale_price_dates_to );
},
input2value: function() {
return {
regular_price: this.$input.find( '[name="regular_price"]' ).val(),
sale_price: this.$input.find( '[name="sale_price"]' ).val(),
sale_price_dates_from: this.$input.find( '[name="sale_price_dates_from"]' ).val(),
sale_price_dates_to: this.$input.find( '[name="sale_price_dates_to"]' ).val()
};
}
} );
var template = '';
var currency_symbol = '$';
if ( typeof woocommerce_admin_meta_boxes != 'undefined' && typeof woocommerce_admin_meta_boxes.currency_format_symbol != 'undefined' ) {
currency_symbol = woocommerce_admin_meta_boxes.currency_format_symbol;
}
template += '<div>';
template += '<div>';
template += '<label>Regular (' + currency_symbol + ')</label>';
template += '<input type="text" class="form-control input-sm" name="regular_price">';
template += '</div>';
template += '<div>';
template += '<label>Sale (' + currency_symbol + ')</label>';
template += '<input type="text" class="form-control input-sm" name="sale_price">';
template += '</div>';
template += '<div class="price-schedule">';
template += '<div>';
template += '<label>Sale from</label>';
template += '<input type="text" placeholder="yyyy-mm-dd" class="form-control input-sm" name="sale_price_dates_from">';
template += '</div>';
template += '<div>';
template += '<label>Sale to</label>';
template += '<input type="text" placeholder="yyyy-mm-dd" class="form-control input-sm" name="sale_price_dates_to">';
template += '</div>';
template += '</div>';
template += '<div>';
template += '<a href="#" class="toggle-price-schedule">Schedule</a>';
template += '</div>';
template += '</div>';
WC_Price.defaults = $.extend( {}, $.fn.editabletypes.abstractinput.defaults, {
tpl: template
} );
$.fn.editabletypes.wc_price = WC_Price;
} ( window.jQuery ) );