1class DoofinderAddToCartError extends Error { 2 constructor(reason, status = "") { 3 const message = "Error adding an item to the cart. Reason: " + reason + ". Status code: " + status; 4 super(message); 5 this.name = "DoofinderAddToCartError"; 6 } 7} 8 9(function ($) { 10 $(document).ready(function () { 11 document.addEventListener("doofinder.cart.add", function (event) { 12 const { item_id, amount, statusPromise } = event.detail; 13 addProductToCart(item_id, amount, statusPromise); 14 }); 15 }); 16 17 function addProductToCart(item_id, amount, statusPromise) { 18 amount = !amount ? 1 : parseInt(amount); 19 item_id = parseInt(item_id); 20 21 $.ajax({ 22 type: "post", 23 url: df_cart.ajax_url, 24 dataType: "json", 25 data: { 26 action: "doofinder_get_product_info", 27 id: item_id, 28 }, 29 success: function (response) { 30 if (response.add_to_cart) { 31 wc_add_to_cart( 32 response.product, 33 response.variation, 34 amount, 35 statusPromise 36 ); 37 } else { 38 statusPromise.reject(new DoofinderAddToCartError("Empty cart response")); 39 window.location = response.product_url; 40 } 41 }, 42 error: function(xhr, ajaxOptions, thrownError) { 43 statusPromise.reject(new DoofinderAddToCartError(thrownError, xhr.status)); 44 } 45 }); 46 } 47 48 function wc_add_to_cart(product_id, variation_id, product_qty, statusPromise) { 49 var data = { 50 action: "doofinder_ajax_add_to_cart", 51 product_id: product_id, 52 quantity: product_qty, 53 variation_id: variation_id, 54 nonce: df_cart.nonce 55 }; 56 57 $fakebutton = $("<input type='button'/>"); 58 59 $(document.body).trigger("adding_to_cart", [$fakebutton, data]); 60 61 $.ajax({ 62 type: "post", 63 url: df_cart.ajax_url, 64 data: data, 65 success: function (response) { 66 if (response.error & response.product_url) { 67 statusPromise.reject(new DoofinderAddToCartError("Invalid product or cart")); 68 window.location = response.product_url; 69 } else if (typeof wc_add_to_cart_params !== "undefined") { 70 //Woocommerce cart is included, trigger add to cart event 71 $(document.body).trigger("added_to_cart", [ 72 response.fragments, 73 response.cart_hash, 74 $fakebutton, 75 ]); 76 statusPromise.resolve("The item has been successfully added to the cart."); 77 } else { 78 //No woocommerce cart, reload the page 79 statusPromise.reject(new DoofinderAddToCartError("No Woocommerce cart was found.")); 80 location.reload(); 81 } 82 }, 83 error: function(xhr, ajaxOptions, thrownError) { 84 statusPromise.reject(new DoofinderAddToCartError(thrownError, xhr.status)); 85 } 86 }); 87 } 88})(jQuery);
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.