woocommerce.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* global Cookies */
  2. jQuery( function( $ ) {
  3. // Orderby
  4. $( '.woocommerce-ordering' ).on( 'change', 'select.orderby', function() {
  5. $( this ).closest( 'form' ).submit();
  6. });
  7. // Target quantity inputs on product pages
  8. $( 'input.qty:not(.product-quantity input.qty)' ).each( function() {
  9. var min = parseFloat( $( this ).attr( 'min' ) );
  10. if ( min >= 0 && parseFloat( $( this ).val() ) < min ) {
  11. $( this ).val( min );
  12. }
  13. });
  14. // Set a cookie and hide the store notice when the dismiss button is clicked
  15. $( '.woocommerce-store-notice__dismiss-link' ).click( function() {
  16. Cookies.set( 'store_notice', 'hidden', { path: '/' } );
  17. $( '.woocommerce-store-notice' ).hide();
  18. });
  19. // Check the value of that cookie and show/hide the notice accordingly
  20. if ( 'hidden' === Cookies.get( 'store_notice' ) ) {
  21. $( '.woocommerce-store-notice' ).hide();
  22. } else {
  23. $( '.woocommerce-store-notice' ).show();
  24. }
  25. // Make form field descriptions toggle on focus.
  26. $( document.body ).on( 'click', function() {
  27. $( '.woocommerce-input-wrapper span.description:visible' ).prop( 'aria-hidden', true ).slideUp( 250 );
  28. } );
  29. $( '.woocommerce-input-wrapper' ).on( 'click', function( event ) {
  30. event.stopPropagation();
  31. } );
  32. $( '.woocommerce-input-wrapper :input' )
  33. .on( 'keydown', function( event ) {
  34. var input = $( this ),
  35. parent = input.parent(),
  36. description = parent.find( 'span.description' );
  37. if ( 27 === event.which && description.length && description.is( ':visible' ) ) {
  38. description.prop( 'aria-hidden', true ).slideUp( 250 );
  39. event.preventDefault();
  40. return false;
  41. }
  42. } )
  43. .on( 'click focus', function() {
  44. var input = $( this ),
  45. parent = input.parent(),
  46. description = parent.find( 'span.description' );
  47. parent.addClass( 'currentTarget' );
  48. $( '.woocommerce-input-wrapper:not(.currentTarget) span.description:visible' ).prop( 'aria-hidden', true ).slideUp( 250 );
  49. if ( description.length && description.is( ':hidden' ) ) {
  50. description.prop( 'aria-hidden', false ).slideDown( 250 );
  51. }
  52. parent.removeClass( 'currentTarget' );
  53. } );
  54. // Common scroll to element code.
  55. $.scroll_to_notices = function( scrollElement ) {
  56. if ( scrollElement.length ) {
  57. $( 'html, body' ).animate( {
  58. scrollTop: ( scrollElement.offset().top - 100 )
  59. }, 1000 );
  60. }
  61. };
  62. });