Açıklama Yok

meta-boxes-coupon.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* global woocommerce_admin_meta_boxes_coupon */
  2. jQuery(function( $ ) {
  3. /**
  4. * Coupon actions
  5. */
  6. var wc_meta_boxes_coupon_actions = {
  7. /**
  8. * Initialize variations actions
  9. */
  10. init: function() {
  11. $( 'select#discount_type' )
  12. .on( 'change', this.type_options )
  13. .trigger( 'change' );
  14. this.insert_generate_coupon_code_button();
  15. $( '.button.generate-coupon-code' ).on( 'click', this.generate_coupon_code );
  16. },
  17. /**
  18. * Show/hide fields by coupon type options
  19. */
  20. type_options: function() {
  21. // Get value
  22. var select_val = $( this ).val();
  23. if ( 'percent' === select_val ) {
  24. $( '#coupon_amount' ).removeClass( 'wc_input_price' ).addClass( 'wc_input_decimal' );
  25. } else {
  26. $( '#coupon_amount' ).removeClass( 'wc_input_decimal' ).addClass( 'wc_input_price' );
  27. }
  28. if ( select_val !== 'fixed_cart' ) {
  29. $( '.limit_usage_to_x_items_field' ).show();
  30. } else {
  31. $( '.limit_usage_to_x_items_field' ).hide();
  32. }
  33. },
  34. /**
  35. * Insert generate coupon code buttom HTML.
  36. */
  37. insert_generate_coupon_code_button: function() {
  38. $( '.post-type-shop_coupon' ).find( '#title' ).after(
  39. '<a href="#" class="button generate-coupon-code">' + woocommerce_admin_meta_boxes_coupon.generate_button_text + '</a>'
  40. );
  41. },
  42. /**
  43. * Generate a random coupon code
  44. */
  45. generate_coupon_code: function( e ) {
  46. e.preventDefault();
  47. var $coupon_code_field = $( '#title' ),
  48. $coupon_code_label = $( '#title-prompt-text' ),
  49. $result = '';
  50. for ( var i = 0; i < woocommerce_admin_meta_boxes_coupon.char_length; i++ ) {
  51. $result += woocommerce_admin_meta_boxes_coupon.characters.charAt(
  52. Math.floor( Math.random() * woocommerce_admin_meta_boxes_coupon.characters.length )
  53. );
  54. }
  55. $result = woocommerce_admin_meta_boxes_coupon.prefix + $result + woocommerce_admin_meta_boxes_coupon.suffix;
  56. $coupon_code_field.trigger( 'focus' ).val( $result );
  57. $coupon_code_label.addClass( 'screen-reader-text' );
  58. }
  59. };
  60. wc_meta_boxes_coupon_actions.init();
  61. });