https://camannucci.com/wp/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.js?ver=8.5.2
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 this.requests = []; 13 this.addRequest = this.addRequest.bind( this ); 14 this.run = this.run.bind( this ); 15 16 $( document.body ) 17 .on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart ) 18 .on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart ) 19 .on( 'added_to_cart', this.updateButton ) 20 .on( 'ajax_request_not_sent.adding_to_cart', this.updateButton ) 21 .on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments ); 22 }; 23 24 /** 25 * Add add to cart event. 26 */ 27 AddToCartHandler.prototype.addRequest = function( request ) { 28 this.requests.push( request ); 29 30 if ( 1 === this.requests.length ) { 31 this.run(); 32 } 33 }; 34 35 /** 36 * Run add to cart events. 37 */ 38 AddToCartHandler.prototype.run = function() { 39 var requestManager = this, 40 originalCallback = requestManager.requests[0].complete; 41 42 requestManager.requests[0].complete = function() { 43 if ( typeof originalCallback === 'function' ) { 44 originalCallback(); 45 } 46 47 requestManager.requests.shift(); 48 49 if ( requestManager.requests.length > 0 ) { 50 requestManager.run(); 51 } 52 }; 53 54 $.ajax( this.requests[0] ); 55 }; 56 57 /** 58 * Handle the add to cart event. 59 */ 60 AddToCartHandler.prototype.onAddToCart = function( e ) { 61 var $thisbutton = $( this ); 62 63 if ( $thisbutton.is( '.ajax_add_to_cart' ) ) { 64 if ( ! $thisbutton.attr( 'data-product_id' ) ) { 65 return true; 66 } 67 68 e.preventDefault(); 69 70 $thisbutton.removeClass( 'added' ); 71 $thisbutton.addClass( 'loading' ); 72 73 // Allow 3rd parties to validate and quit early. 74 if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) { 75 $( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] ); 76 return true; 77 } 78 79 var data = {}; 80 81 // Fetch changes that are directly added by calling $thisbutton.data( key, value ) 82 $.each( $thisbutton.data(), function( key, value ) { 83 data[ key ] = value; 84 }); 85 86 // Fetch data attributes in $thisbutton. Give preference to data-attributes because they can be directly modified by javascript 87 // while `.data` are jquery specific memory stores. 88 $.each( $thisbutton[0].dataset, function( key, value ) { 89 data[ key ] = value; 90 }); 91 92 // Trigger event. 93 $( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] ); 94 95 e.data.addToCartHandler.addRequest({ 96 type: 'POST', 97 url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), 98 data: data, 99 success: function( response ) { 100 if ( ! response ) { 101 return; 102 } 103 104 if ( response.error && response.product_url ) { 105 window.location = response.product_url; 106 return; 107 } 108 109 // Redirect to cart option 110 if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) { 111 window.location = wc_add_to_cart_params.cart_url; 112 return; 113 } 114 115 // Trigger event so themes can refresh other areas. 116 $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); 117 }, 118 dataType: 'json' 119 }); 120 } 121 }; 122 123 /** 124 * Update fragments after remove from cart event in mini-cart. 125 */ 126 AddToCartHandler.prototype.onRemoveFromCart = function( e ) { 127 var $thisbutton = $( this ), 128 $row = $thisbutton.closest( '.woocommerce-mini-cart-item' ); 129 130 e.preventDefault(); 131 132 $row.block({ 133 message: null, 134 overlayCSS: { 135 opacity: 0.6 136 } 137 }); 138 139 e.data.addToCartHandler.addRequest({ 140 type: 'POST', 141 url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ), 142 data: { 143 cart_item_key : $thisbutton.data( 'cart_item_key' ) 144 }, 145 success: function( response ) { 146 if ( ! response || ! response.fragments ) { 147 window.location = $thisbutton.attr( 'href' ); 148 return; 149 } 150 151 $( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); 152 }, 153 error: function() { 154 window.location = $thisbutton.attr( 'href' ); 155 return; 156 }, 157 dataType: 'json' 158 }); 159 }; 160 161 /** 162 * Update cart page elements after add to cart events. 163 */ 164 AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) { 165 $button = typeof $button === 'undefined' ? false : $button; 166 167 if ( $button ) { 168 $button.removeClass( 'loading' ); 169 170 if ( fragments ) { 171 $button.addClass( 'added' ); 172 } 173 174 // View cart text. 175 if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) { 176 $button.after( '<a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' + 177 wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' ); 178 } 179 180 $( document.body ).trigger( 'wc_cart_button_updated', [ $button ] ); 181 } 182 }; 183 184 /** 185 * Update fragments after add to cart events. 186 */ 187 AddToCartHandler.prototype.updateFragments = function( e, fragments ) { 188 if ( fragments ) { 189 $.each( fragments, function( key ) { 190 $( key ) 191 .addClass( 'updating' ) 192 .fadeTo( '400', '0.6' ) 193 .block({ 194 message: null, 195 overlayCSS: { 196 opacity: 0.6 197 } 198 }); 199 }); 200 201 $.each( fragments, function( key, value ) { 202 $( key ).replaceWith( value ); 203 $( key ).stop( true ).css( 'opacity', '1' ).unblock(); 204 }); 205 206 $( document.body ).trigger( 'wc_fragments_loaded' ); 207 } 208 }; 209 210 /** 211 * Init AddToCartHandler. 212 */ 213 new AddToCartHandler(); 214});
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.