Created
October 27, 2015 14:20
-
-
Save gicolek/2b0f1a8aaaef6f04f45c to your computer and use it in GitHub Desktop.
Dynamic WooCommerce attributes reload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Handler for Widget Filters | |
* ********************************************************************************** | |
*/ | |
var obj = {}; | |
var obj_ajax = {}; | |
var out = ''; | |
// on each widget input click | |
$('.widget_attributes input').click(function (e) { | |
var name = ''; | |
var val = ''; | |
// iterate over each input | |
$('.widget_attributes input').each(function (e) { | |
// collect the input value and name | |
val = $(this).val(); | |
name = $(this).attr('name'); | |
// if the object is not an array make sure to instantiate it | |
if ($.type(obj[name]) !== 'array') { | |
obj[name] = []; | |
} | |
// if the checkbox was checked make sure to push the value if absent | |
if ($(this).is(':checked')) { | |
if ($.inArray(val, obj[name]) === -1) { | |
obj[name].push( | |
val | |
); | |
} | |
} | |
// remove the value from the array if unchecked and present | |
else { | |
var index = $.inArray(val, obj[name]); | |
if (index !== -1) { | |
obj[name].splice(index, 1); | |
} | |
} | |
}); | |
// iterate over each object elements | |
$.each(obj, function (key, value) { | |
var ajax_key = key.replace('pa_', ''); | |
// destroy empty arrays | |
if ($(this).length === 0) { | |
delete(obj[key]); | |
delete(obj_ajax['filter_' + ajax_key]); | |
delete(obj_ajax['query_type_' + ajax_key]); | |
} | |
else { | |
// construct ajax object elements | |
obj_ajax['filter_' + ajax_key] = value.join(','); | |
obj_ajax['query_type_' + ajax_key] = 'and'; | |
} | |
}); | |
$('.products').html('Loading ...'); | |
$.get(window.location.pathname, obj_ajax, function (response) { | |
out = $(response).find('.products').html(); | |
$('.products').html(out); | |
var stateObj = { page: "Shop" }; | |
window.history.pushState(stateObj, "Shop", $(this)[0].url); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment