I changed shop page pagination and added custom code to load products using ajax on page scroll. My site is multilingual and i am using WPML. The code is working fine in default language but in other language instead of showing products of that language its showing me products of default language i.e. english. Means i want to show translated products correspond to specific language.
Here is my code ajax code :
var $ = jQuery;
var flag=1;
var limit=0;
$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height()){
limit++;
if(flag==1)
{
$(".lazy_lode_img").css('display','block');//display loading image
$.ajax({
url:"<?php echo get_stylesheet_directory_uri();?>/ajax.php", // ajax page
type:'POST',
data:{ 'paged':limit}, // send page no
dataType:"html",
success: function(product_data){
//alert(product_data);
if(product_data!=0)
{
$( ".shop-products.row.grid-view" ).append(product_data);
$(".lazy_lode_img").css('display','none');
}
else
{
flag=0;
// $('#k_test').append('<div class="news" id="no_news" style="text-align:center;">NO MORE PRODUCT</div>');
$(".lazy_lode_img").remove();
}
}
});
}
}
});
And here is my php code:
$page_no = $_POST['paged'];
$post_per_page=6;
$args = array(
'posts_per_page' => $post_per_page,//set post per page
'paged' => $page_no,//set offset for limit
'post_type' => 'product',
'post_status'=>'publish',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
/*$price = get_post_meta( $query->post->ID, '_regular_price', true );
$price=round($price,2);
if ((int) $price == $price)
{
$price=$price.'.00';
}*/
?>
<div class="item-col col-xs-6 col-md-4 col-sm-4 post-6303 product type-product status-publish has-post-thumbnail product_cat-all-products product_cat-tarts instock shipping-taxable purchasable product-type-simple">
<div class="product-wrapper product-wrapper2">
<div class="list-col4">
<div class="product-image">
<a class="twoimg" href="<?php echo get_permalink( $query->post->ID );?>" title="Blueberry Frangipane Tart">
<?php echo get_the_post_thumbnail( $query->post->ID, 'shop_catalog' );?>
</a>
</div>
<div class="home-product-title">
<h2 class="product-name">
<a href="<?php echo get_permalink( $query->post->ID );?>" style="color:black;"><?php echo $query->post->post_title;?></a>
</h2>
<span class="arrow-img"></span>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<?php
endwhile;
}
else
{
echo '0';
}
I don't understand where should i pass language code so that it will fetch products of current selected language on front-end.
After spending time on it finally i found the solution.
I just added an argument
'lang' => $current_language
Now my query arguments looks like:
$args = array(
'posts_per_page' => $post_per_page,//set post per page
'paged' => $page_no,//set offset for limit
'post_type' => 'product',
'post_status'=>'publish',
'lang' => $current_language
);
And it works like charm.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments