2019-07-09 19:46:17 +00:00
|
|
|
/* global woocommerce_admin_meta_boxes_coupon */
|
2015-07-10 04:32:30 +00:00
|
|
|
jQuery(function( $ ) {
|
2014-07-08 13:14:29 +00:00
|
|
|
|
2015-07-10 04:32:30 +00:00
|
|
|
/**
|
|
|
|
* Coupon actions
|
|
|
|
*/
|
|
|
|
var wc_meta_boxes_coupon_actions = {
|
2014-07-08 13:14:29 +00:00
|
|
|
|
2015-07-10 04:32:30 +00:00
|
|
|
/**
|
|
|
|
* Initialize variations actions
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
$( 'select#discount_type' )
|
|
|
|
.on( 'change', this.type_options )
|
|
|
|
.change();
|
2019-07-03 20:48:30 +00:00
|
|
|
|
2019-07-09 19:46:17 +00:00
|
|
|
this.insert_generate_coupon_code_button();
|
2019-07-03 20:48:30 +00:00
|
|
|
$( '.button.generate-coupon-code' ).on( 'click', this.generate_coupon_code );
|
2015-07-10 04:32:30 +00:00
|
|
|
},
|
2014-07-08 13:14:29 +00:00
|
|
|
|
2015-07-10 04:32:30 +00:00
|
|
|
/**
|
|
|
|
* Show/hide fields by coupon type options
|
|
|
|
*/
|
|
|
|
type_options: function() {
|
|
|
|
// Get value
|
|
|
|
var select_val = $( this ).val();
|
2014-07-08 13:14:29 +00:00
|
|
|
|
2019-03-25 14:56:27 +00:00
|
|
|
if ( 'percent' === select_val ) {
|
|
|
|
$( '#coupon_amount' ).removeClass( 'wc_input_price' ).addClass( 'wc_input_decimal' );
|
|
|
|
} else {
|
|
|
|
$( '#coupon_amount' ).removeClass( 'wc_input_decimal' ).addClass( 'wc_input_price' );
|
|
|
|
}
|
|
|
|
|
2016-12-14 11:07:08 +00:00
|
|
|
if ( select_val !== 'fixed_cart' ) {
|
2015-07-10 04:32:30 +00:00
|
|
|
$( '.limit_usage_to_x_items_field' ).show();
|
|
|
|
} else {
|
|
|
|
$( '.limit_usage_to_x_items_field' ).hide();
|
|
|
|
}
|
2019-07-09 19:46:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert generate coupon code buttom HTML.
|
|
|
|
*/
|
|
|
|
insert_generate_coupon_code_button: function() {
|
|
|
|
$( '.post-type-shop_coupon' ).find( '#title' ).after(
|
|
|
|
'<a href="#" class="button generate-coupon-code">' + woocommerce_admin_meta_boxes_coupon.generate_button_text + '</a>'
|
|
|
|
);
|
|
|
|
},
|
2019-07-03 20:48:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a random coupon code
|
|
|
|
*/
|
|
|
|
generate_coupon_code: function( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
var $coupon_code_field = $( '#title' ),
|
|
|
|
$coupon_code_label = $( '#title-prompt-text' ),
|
|
|
|
$result = '';
|
2019-07-09 19:46:17 +00:00
|
|
|
for ( var i = 0; i < woocommerce_admin_meta_boxes_coupon.char_length; i++ ) {
|
2019-07-03 20:48:30 +00:00
|
|
|
$result += woocommerce_admin_meta_boxes_coupon.characters.charAt(
|
|
|
|
Math.floor( Math.random() * woocommerce_admin_meta_boxes_coupon.characters.length )
|
|
|
|
);
|
|
|
|
}
|
2019-07-04 07:07:02 +00:00
|
|
|
$result = woocommerce_admin_meta_boxes_coupon.prefix + $result + woocommerce_admin_meta_boxes_coupon.suffix;
|
2019-07-03 20:48:30 +00:00
|
|
|
$coupon_code_field.focus().val( $result );
|
|
|
|
$coupon_code_label.addClass( 'screen-reader-text' );
|
2015-07-10 04:32:30 +00:00
|
|
|
}
|
|
|
|
};
|
2014-07-08 13:14:29 +00:00
|
|
|
|
2015-07-10 04:32:30 +00:00
|
|
|
wc_meta_boxes_coupon_actions.init();
|
2014-08-31 06:25:22 +00:00
|
|
|
});
|