Untitled

 avatar
unknown
plain_text
a year ago
4.2 kB
3
Indexable
function setHoverColorTable(quantity = 'no'){

	// remove all colors from table
	$('.b2bking_has_color').removeClass('b2bking_has_color');
	// get product id from table
	if ($('.b2bking_shop_table').attr('class') !== undefined){
		var classList = $('.b2bking_shop_table').attr('class').split(/\s+/);
		var productid = 0;
		$.each(classList, function(index, item) {
			// foreach line if it has selected class, get selected product ID 
		    if (item.includes('b2bking_productid_')) {
		    	productid = parseInt(item.split('_')[2]);
		    }
		});

		// get input quantity
		if ($('input[name=quantity]').val() !== undefined){
			var inputQuantity = parseInt($('input[name=quantity]').val());
		} else if ($('select[name=quantity]').val() !== undefined){
			var inputQuantity = parseInt($('select[name=quantity]').val());
		}
		if (quantity !== 'no'){
			if (typeof inputQuantity !== 'undefined') {
				inputQuantity = parseInt(quantity);
			} else {
				var inputQuantity = parseInt(quantity);
			}
		}

		// get cart item quantity
		var cartQuantity = 0;
		if (parseInt(b2bking_display_settings.add_cart_quantity_tiered_table) === 1){

			if (b2bking_display_settings.cart_quantities[productid] !== undefined){
				cartQuantity = parseInt(b2bking_display_settings.cart_quantities[productid]);
			}
			if (parseInt(b2bking_display_settings.cart_quantities_cartqty) !== 0){
				cartQuantity = parseInt(b2bking_display_settings.cart_quantities_cartqty);
			}
		}


		// calculate total quantity of the item
		var totalQuantity = inputQuantity + cartQuantity;


		// go through all ranges and check quantity. 
		// first set it to original price
		$('.b2bking_tiered_active_price').text($('.summary .b2bking_tiered_range_replaced:first').text().split(' – ')[1]);
		// if can't be found,
		if ($('.summary .b2bking_tiered_range_replaced:first').text().split(' – ')[1] === undefined){
			$('.b2bking_tiered_active_price').text($('.b2bking_tiered_range_replaced:first').text().split(' – ')[1]);
		}

		// if exists a specific productid of the page
		if (parseInt(b2bking_display_settings.productid) !== 0){
			var rangereplaced = jQuery('.b2bking_tiered_price_range_replaced_' + b2bking_display_settings.productid + ':first').text();
			if (rangereplaced !== ''){
				$('.b2bking_tiered_active_price').text(rangereplaced.split(' – ')[1]);
			}
		}

		$('.b2bking_shop_table.b2bking_productid_'+productid+' tr td:nth-child(1)').each(function(){
			let rangeText = $(this).data('range');
			let values = rangeText.split(' - ');

			if (values.length === 2){
				// is of form 123 - 456
				let first = parseInt(values[0]);
				let second = parseInt(values[1]);
				if (totalQuantity >= first && totalQuantity <= second){
					// set color
					$(this).parent().find('td').addClass('b2bking_has_color');
					if (parseInt(b2bking_display_settings.is_enabled_discount_table) === 1){
						$('.b2bking_tiered_active_price').text($(this).parent().find('td:nth-child(3)').text());
					} else {
						$('.b2bking_tiered_active_price').text($(this).parent().find('td:nth-child(2)').text());

					}
				}
			} else if (!rangeText.includes('+')){
				// exception if the user enters 1 as a quantity in the table
				if (totalQuantity === parseInt(rangeText)){
					$(this).parent().find('td').addClass('b2bking_has_color');
					if (parseInt(b2bking_display_settings.is_enabled_discount_table) === 1){
						$('.b2bking_tiered_active_price').text($(this).parent().find('td:nth-child(3)').text());
					} else {
						$('.b2bking_tiered_active_price').text($(this).parent().find('td:nth-child(2)').text());
					}
				}
			} else {
				// is of form 456+
				let valuePlus = parseInt(rangeText.split('+')[0]);
				if (totalQuantity >= valuePlus){
					// set color
					$(this).parent().find('td').addClass('b2bking_has_color');
					if (parseInt(b2bking_display_settings.is_enabled_discount_table) === 1){
						$('.b2bking_tiered_active_price').text($(this).parent().find('td:nth-child(3)').text());
					} else {
						$('.b2bking_tiered_active_price').text($(this).parent().find('td:nth-child(2)').text());
					}
				}
			}
		});

		$( document.body ).trigger( 'b2bking_set_hover_finish' ); 
	}
}