1jQuery(document).ready(function ($) { 2 "use strict"; 3 // Select/Un-select product 4 $(document).on('change', '.item-product input[type="checkbox"]', function (e) { 5 var $this = $(this); 6 var $thisWrap = $this.closest('.item-products-wrap'); 7 var $thisWpbingoWrap = $thisWrap.closest('.buy-together-wrap'); 8 var $thisProductsBtWrap = $thisWpbingoWrap.find('.item-products-wrap'); 9 var total_price = 0; 10 var total_items = 0; 11 12 $thisWrap.find('.item-product input[type="checkbox"]').each(function () { 13 var this_product_id = $(this).attr('data-product_id'); 14 if ($(this).is(':checked')) { 15 var this_price = parseFloat($(this).attr('data-price')); 16 if (!isNaN(this_price)) { 17 total_price += this_price; 18 } 19 total_items++; 20 $thisProductsBtWrap.find('.item-product[data-product_id="' + this_product_id + '"]').removeClass('buy-together-hidden'); 21 } 22 else { 23 $thisProductsBtWrap.find('.item-product[data-product_id="' + this_product_id + '"]:not(.buy-together-content)').addClass('buy-together-hidden'); 24 } 25 26 }); 27 28 var total_price_html = bwp_woo_format_money(total_price); 29 $thisWpbingoWrap.find('.total-price-html').html(total_price_html); 30 $thisWpbingoWrap.find('.for-items-text').text(buy_together['text']['for_num_of_items'].replace('{{number}}', total_items)); 31 32 }); 33 34 // Add all selected products to cart 35 $(document).on('click', '.buy-together-add-all-to-cart', function (e) { 36 var $this = $(this); 37 if ($this.is('.adding-to-cart')) { 38 return false; 39 } 40 41 var $thisWpbingoWrap = $this.closest('.buy-together-wrap'); 42 var $itemsWrap = $thisWpbingoWrap.find('.item-products-wrap'); 43 var i = 0; 44 45 // Check selected products 46 if (!$itemsWrap.find('.item-product input[type="checkbox"]:checked').length) { 47 var error_msg_html = '<div class="buy-together-error-message woocommerce-error">' + buy_together['text']['no_product_selected_text'] + '</div>'; 48 if (!$thisWpbingoWrap.find('.buy-together-messages-wrap').length) { 49 $thisWpbingoWrap.prepend('<div class="buy-together-messages-wrap"></div>'); 50 } 51 $thisWpbingoWrap.find('.buy-together-messages-wrap').html(error_msg_html); 52 return false; 53 } 54 55 $itemsWrap.find('.item-product').each(function () { 56 $(this).addClass('item-product-' + i).attr('data-item_num', i).find('input[type="checkbox"]').prop('disabled', true); 57 i++; 58 }); 59 60 $this.addClass('adding-to-cart disabled'); 61 $this.text(buy_together['text']['adding_to_cart_text']); 62 $thisWpbingoWrap.find('.buy-together-messages-wrap').html(''); 63 bwp_add_to_cart($itemsWrap, 0); 64 65 }); 66 67 function bwp_add_to_cart($itemsWrap, item_num) { 68 var next_item_num = item_num + 1; 69 var $productItem = $itemsWrap.find('.item-product-' + item_num); 70 var $nextProductItem = $itemsWrap.find('.item-product-' + next_item_num); 71 var $wpbingoWrap = $itemsWrap.closest('.buy-together-wrap'); 72 73 if (!$productItem.length) { 74 $itemsWrap.closest('.buy-together-wrap').find('.buy-together-add-all-to-cart').removeClass('adding-to-cart disabled').text(buy_together['text']['add_to_cart_text']); 75 $itemsWrap.find('.item-product:not(.buy-together-main-item) input[type="checkbox"]').prop('disabled', false); 76 bwp_display_add_to_cart_messages($itemsWrap); 77 bwp_reset_add_to_cart_count_success_fail($itemsWrap); 78 $(document.body).trigger('wc_fragment_refresh'); 79 return; 80 } 81 82 if (!$productItem.find('input[type="checkbox"]').is(':checked')) { 83 if ($nextProductItem.length) { 84 bwp_add_to_cart($itemsWrap, next_item_num); 85 } 86 else { 87 $itemsWrap.closest('.buy-together-wrap').find('.buy-together-add-all-to-cart').removeClass('adding-to-cart disabled').text(buy_together['text']['add_to_cart_text']); 88 $itemsWrap.find('.item-product:not(.buy-together-main-item) input[type="checkbox"]').prop('disabled', false); 89 bwp_display_add_to_cart_messages($itemsWrap); 90 bwp_reset_add_to_cart_count_success_fail($itemsWrap); 91 $(document.body).trigger('wc_fragment_refresh'); 92 return; 93 } 94 } 95 else { 96 var product_id = $productItem.attr('data-product_id'); 97 var data = { 98 action: 'woocommerce_add_to_cart', 99 product_id: product_id 100 }; 101 102 $.post(buy_together['ajaxurl'], data, function (response) { 103 if ($nextProductItem.length) { 104 bwp_count_add_to_cart_success_fail($itemsWrap, response); 105 bwp_add_to_cart($itemsWrap, next_item_num); 106 } 107 else { 108 bwp_count_add_to_cart_success_fail($itemsWrap, response); 109 110 $wpbingoWrap.find('.buy-together-add-all-to-cart').removeClass('adding-to-cart disabled').text(buy_together['text']['add_to_cart_text']); 111 $itemsWrap.find('.item-product:not(.buy-together-main-item) input[type="checkbox"]').prop('disabled', false); 112 113 bwp_display_add_to_cart_messages($itemsWrap); 114 bwp_reset_add_to_cart_count_success_fail($wpbingoWrap); 115 116 // $itemsWrap.closest('.buy-together-wrap').html 117 } 118 }); 119 } 120 } 121 122 function bwp_display_add_to_cart_messages($itemsWrap) { 123 var $wpbingoWrap = $itemsWrap.closest('.buy-together-wrap'); 124 var count_success = parseInt($wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_success')); 125 var count_fail = parseInt($wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_fail')); 126 var message_success = buy_together['text']['add_to_cart_success'].replace('{{number}}', count_success); 127 var message_fail = ''; 128 if (count_fail == 1) { 129 message_fail = buy_together['text']['add_to_cart_fail_single'].replace('{{number}}', count_fail); 130 } 131 if (count_fail > 1) { 132 message_fail = buy_together['text']['add_to_cart_fail_plural'].replace('{{number}}', count_fail); 133 } 134 var count_success_html = ''; 135 var count_fail_html = ''; 136 $(document.body).trigger('wc_fragment_refresh'); 137 if (!$wpbingoWrap.find('.buy-together-messages-wrap').length) { 138 $wpbingoWrap.prepend('<div class="buy-together-messages-wrap"></div>'); 139 } 140 if ($.trim(message_success) != '') { 141 var view_cart_html = '<a class="button wc-forward" href="' + buy_together['cart_url'] + '">' + buy_together['text']['view_cart'] + '</a>'; 142 count_success_html = '<div class="buy-together-success-message woocommerce-message">' + view_cart_html + message_success + '</div>'; 143 } 144 if ($.trim(message_fail) != '') { 145 count_fail_html = '<div class="buy-together-error-message woocommerce-error">' + message_fail + '</div>'; 146 } 147 $wpbingoWrap.find('.buy-together-messages-wrap').html(count_success_html + count_fail_html); 148 } 149 150 function bwp_count_add_to_cart_success_fail($itemsWrap, response) { 151 var count_fail; 152 var count_success; 153 var $wpbingoWrap = $itemsWrap.closest('.buy-together-wrap'); 154 if (response.hasOwnProperty('error')) { 155 if (response['error']) { 156 count_fail = parseInt($wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_fail')); 157 if (isNaN(count_fail)) { 158 count_fail = 0; 159 } 160 count_fail++; 161 $wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_fail', count_fail); 162 } 163 else { 164 count_success = parseInt($wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_success')); 165 if (isNaN(count_success)) { 166 count_success = 0; 167 } 168 count_success++; 169 $wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_success', count_success); 170 } 171 } 172 else { 173 count_success = parseInt($wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_success')); 174 if (isNaN(count_success)) { 175 count_success = 0; 176 } 177 count_success++; 178 $wpbingoWrap.find('.buy-together-add-all-to-cart').attr('data-count_success', count_success); 179 } 180 } 181 182 function bwp_reset_add_to_cart_count_success_fail($itemsWrap) { 183 $itemsWrap.find('.buy-together-add-all-to-cart').attr('data-count_success', 0).attr('data-count_fail', 0); 184 } 185 186 /** 187 * 188 * @param $form 189 * @param response 190 * @param position top or bottom. 191 */ 192 function bwp_display_multi_messages($form, response, position) { 193 $form.find('.buy-together-message').remove(); 194 195 var msg_class = ''; 196 197 if (response['err'] === 'yes') { 198 msg_class += 'alert-danger'; 199 } 200 else { 201 msg_class += 'alert-success'; 202 } 203 204 if ($.type(response['message']) === 'string') { 205 if (response['message'] !== '') { 206 if (position === 'top') { 207 $form.prepend('<div class="buy-together-message alert ' + msg_class + '">' + response['message'] + '</div>'); 208 } 209 else { 210 $form.append('<div class="buy-together-message alert ' + msg_class + '">' + response['message'] + '</div>'); 211 } 212 } 213 } 214 else { 215 $.each(response['message'], function (index, item) { 216 if (position === 'top') { 217 $form.prepend('<div class="buy-together-message alert ' + msg_class + '">' + item + '</div>'); 218 } 219 else { 220 $form.append('<div class="buy-together-message alert ' + msg_class + '">' + item + '</div>'); 221 } 222 }); 223 } 224 } 225 226 function bwp_woo_format_money(number) { 227 return bwp_format_money(number, buy_together['price_thousand_separator'], buy_together['price_decimal_separator'], buy_together['price_decimals'], buy_together['currency_symbol'], buy_together['price_format']); 228 } 229 230 function bwp_format_money(number, thousand_sep, decimal_sep, tofixed, symbol, woo_price_format) { 231 var before_text = ''; 232 var after_text = ''; 233 number = number || 0; 234 tofixed = !isNaN(tofixed = Math.abs(tofixed)) ? tofixed : 2; 235 symbol = symbol !== undefined ? symbol : "$"; 236 thousand_sep = thousand_sep || ","; 237 decimal_sep = decimal_sep || "."; 238 var negative = number < 0 ? "-" : "", 239 i = parseInt(number = Math.abs(+number || 0).toFixed(tofixed), 10) + "", 240 j = (j = i.length) > 3 ? j % 3 : 0; 241 242 symbol = '<span class="woocommerce-Price-currencySymbol">' + symbol + '</span>'; 243 244 switch (woo_price_format) { 245 case '%1$s%2$s': 246 //left 247 before_text += symbol; 248 break; 249 case '%1$s %2$s': 250 //left with space 251 before_text += symbol + ' '; 252 break; 253 case '%2$s%1$s': 254 //right 255 after_text += symbol; 256 break; 257 case '%2$s %1$s': 258 //right with space 259 after_text += ' ' + symbol; 260 break; 261 default: 262 //default 263 before_text += symbol; 264 } 265 266 267 var money_return = before_text + 268 negative + (j ? i.substr(0, j) + thousand_sep : "" ) + 269 i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand_sep) + 270 (tofixed ? decimal_sep + Math.abs(number - i).toFixed(tofixed).slice(2) : "") + 271 after_text; 272 273 if (buy_together['wc_tax_enabled']) { 274 money_return += ' <small class="woocommerce-Price-taxLabel tax_label">' + buy_together['ex_tax_or_vat'] + '</small>'; 275 } 276 277 money_return = '<span class="woocommerce-Price-amount amount">' + money_return + '</span>'; 278 279 return money_return; 280 } 281});
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.