Skip to content

Instantly share code, notes, and snippets.

@JudeRosario
Created February 5, 2015 13:43
Show Gist options
  • Save JudeRosario/3858258af0add5953758 to your computer and use it in GitHub Desktop.
Save JudeRosario/3858258af0add5953758 to your computer and use it in GitHub Desktop.
MarketPress Feature : Find product by Tag
// Modify two functions in your ajax-cart.js file.
// It can be found in the marketpress-includes/js folder.
// Simply copy/paste and //replace the 2 functions there with the code from here
// `mp_store_listeners` and `get_and_insert_products`
//general store listeners (e.g. pagination, etc)
function mp_store_listeners(){
// on next/prev link click, get page number and update products
$(document).on('click', '#mp_product_nav a', function(e){
e.preventDefault();
var hrefParts = $(this).attr('href').split('#'),
qs = parse_query(hrefParts[1]);
var url = $(location).attr('href');
if (url.indexOf("tag") > -1) {
var myregex = /tag(?=[/])(.*)(?=[/])/g;
var myArray = myregex.exec(url);
var tag = myArray[1].replace("/","");
}
if (typeof tag !== 'undefined')
{
get_and_insert_products($('.mp_product_list_refine').serialize() +'&tag=' + tag + '&page=' + qs['page']);
}
else {
get_and_insert_products($('.mp_product_list_refine').serialize() + '&page=' + qs['page']);
}
});
}
// get products via ajax, insert into DOM with new pagination links
function get_and_insert_products(query_string){
ajax_loading(true);
$.post(MP_Ajax.ajaxUrl, 'action=get_products_list&' + query_string, function(data) {
var qs = parse_query(query_string);
if('tag' in qs) {
hash = 'product_category=' + qs['product_category']
+ '&order=' + $('select[name="order"]').val()
+ '&tag=' + qs['tag']
+ '&page=' + qs['page'];
}
else{
hash = 'product_category='
+ qs['product_category']
+ '&order=' + $('select[name="order"]').val()
+ '&page=' + qs['page'];
}
ajax_loading(false);
$('#mp_product_nav').remove();
$('#mp_product_list').first().replaceWith(data.products);
location.hash = hash;
// scroll to top of list
var pos = $('a[name="mp-product-list-top"]').offset();
$('body,html').animate({ scrollTop: pos.top - 20 });
});
}
// Add this snippet in your marketpress.php file in the get_products_list function
if ( isset($_POST['tag'])) {
$args['tag'] = $_POST['tag'];
}
// Add it just above these lines.
$ret['products'] = mp_list_products($args);
wp_send_json($ret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment