1/* global wc_add_to_cart_params */ 2jQuery( function( $ ) { 3 4 if ( typeof wc_add_to_cart_params === 'undefined' ) { 5 return false; 6 } 7 8 /** 9 * AddToCartHandler class. 10 */ 11 var AddToCartHandler = function() { 12 $( document ) 13 .on( 'click', '.add_to_cart_button', this.onAddToCart ) 14 .on( 'added_to_cart', this.updateButton ) 15 .on( 'added_to_cart', this.updateCartPage ) 16 .on( 'added_to_cart', this.updateFragments ); 17 }; 18 19 /** 20 * Handle the add to cart event. 21 */ 22 AddToCartHandler.prototype.onAddToCart = function( e ) { 23 var $thisbutton = $( this ); 24 25 if ( $thisbutton.is( '.ajax_add_to_cart' ) ) { 26 if ( ! $thisbutton.attr( 'data-product_id' ) ) { 27 return true; 28 } 29 30 e.preventDefault(); 31 32 $thisbutton.removeClass( 'added' ); 33 $thisbutton.addClass( 'loading' ); 34 35 var data = {}; 36 37 $.each( $thisbutton.data(), function( key, value ) { 38 data[ key ] = value; 39 }); 40 41 // Trigger event. 42 $( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] ); 43 44 // Ajax action. 45 $.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) { 46 if ( ! response ) { 47 return; 48 } 49 50 if ( response.error && response.product_url ) { 51 window.location = response.product_url; 52 return; 53 } 54 55 // Redirect to cart option 56 if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) { 57 window.location = wc_add_to_cart_params.cart_url; 58 return; 59 } 60 61 // Trigger event so themes can refresh other areas. 62 $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); 63 }); 64 } 65 }; 66 67 /** 68 * Update cart page elements after add to cart events. 69 */ 70 AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) { 71 $button = typeof $button === 'undefined' ? false : $button; 72 73 if ( $button ) { 74 $button.removeClass( 'loading' ); 75 $button.addClass( 'added' ); 76 77 // View cart text. 78 if ( ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) { 79 $button.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' + 80 wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' ); 81 } 82 83 $( document.body ).trigger( 'wc_cart_button_updated', [ $button ] ); 84 } 85 }; 86 87 /** 88 * Update cart page elements after add to cart events. 89 */ 90 AddToCartHandler.prototype.updateCartPage = function() { 91 var page = window.location.toString().replace( 'add-to-cart', 'added-to-cart' ); 92 93 $( '.shop_table.cart' ).load( page + ' .shop_table.cart:eq(0) > *', function() { 94 $( '.shop_table.cart' ).stop( true ).css( 'opacity', '1' ).unblock(); 95 $( document.body ).trigger( 'cart_page_refreshed' ); 96 }); 97 98 $( '.cart_totals' ).load( page + ' .cart_totals:eq(0) > *', function() { 99 $( '.cart_totals' ).stop( true ).css( 'opacity', '1' ).unblock(); 100 $( document.body ).trigger( 'cart_totals_refreshed' ); 101 }); 102 }; 103 104 /** 105 * Update fragments after add to cart events. 106 */ 107 AddToCartHandler.prototype.updateFragments = function( e, fragments ) { 108 if ( fragments ) { 109 $.each( fragments, function( key ) { 110 $( key ) 111 .addClass( 'updating' ) 112 .fadeTo( '400', '0.6' ) 113 .block({ 114 message: null, 115 overlayCSS: { 116 opacity: 0.6 117 } 118 }); 119 }); 120 121 $.each( fragments, function( key, value ) { 122 $( key ).replaceWith( value ); 123 $( key ).stop( true ).css( 'opacity', '1' ).unblock(); 124 }); 125 126 $( document.body ).trigger( 'wc_fragments_loaded' ); 127 } 128 }; 129 130 /** 131 * Init AddToCartHandler. 132 */ 133 new AddToCartHandler(); 134});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.