jeudi 26 février 2015

Woocommerce Ajax Pagination


I am creating plugin for ajax pagination in woocommerce product archive pages.


When i click page number navigation, ajax works but some classes for product list are not added hence product list does not display correctly and title also changes.


My site: http://ift.tt/1FyU1VP


Here is the code I am using in my plugin:



add_action( 'wp_enqueue_scripts', 'wap_archive_product_styles' );
add_action( "wp_ajax_wap_get_wc_paginated", "wap_get_wc_paginated" );
add_action( "wp_ajax_nopriv_wap_get_wc_paginated", "wap_get_wc_paginated" );

function wap_archive_product_styles() {
global $wp_query;
wp_enqueue_script( 'wap', plugins_url( 'scripts.js', __FILE__), array( 'jquery' ), false, true );
wp_localize_script( 'wap', 'wc_pagination_ajax', array(
'query_vars' => json_encode( $wp_query->query_vars )
));
}

function wap_get_wc_paginated() {
$query_vars = json_decode( stripslashes( $_POST['query_vars'] ), true );
$query_vars['paged'] = $_POST['page'];
$GLOBALS['wp_the_query'] = $GLOBALS['wp_query'] = new WP_Query( $query_vars );

woocommerce_content();
die();
}


Here is the javascript:



jQuery(".main").on("click", ".pagination.wc-ajax li a", function(e) {
e.preventDefault();
var $ = jQuery;
var wrap = document.querySelector('.main .wrap');

var obj = $( this );
url = obj.attr( "href" );
page = url.match( /paged=(\d+)/ ) ? url.match( /paged=(\d+)/ )[1] : url.match( /\/page\/(\d+)/ )[1];

jQuery.ajax({
type : "post",
dataType : 'html',
url : pagination_ajax.ajaxurl,
data : {action: "wap_get_wc_paginated", query_vars: wc_pagination_ajax.query_vars, page: page },
success: function(response) {
wrap.innerHTML = response;
}
});
});


What am I doing wrong? Please help. Thank you very much.


(Note: I dont want to just change css)





Aucun commentaire:

Enregistrer un commentaire