How to get woocommerce products of current language with WP_Query?

dev tester

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.

dev tester

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.

edited at
0

Comments

0 comments
Login to comment

Related

Exclude WooCommerce products with no reviews from a WP_Query

Woocommerce wp_query get order by ID

How to get the current language in Android?

WooCommerce get products by attribute query

Get WooCommerce featured products in a WP_Query

Get WooCommerce Subscription product type and specific category in a WP_Query

WP_Query issue when trying to get out of stock products in WooCommerce

Get sku from Woocommerce product bookings in a WP_Query

Remove Woocommerce free products from a WP_Query loop

Get all Woocommerce products from current product category term Id in a WP_Query

Get products which are visible in catalog in a WP_query on Woocommerce

Show only WooCommerce in stock products with a WP_Query

Woocommerce, get products by category with wp query

How to query products by their categories woocommerce

Limit Woocommerce featured products in a WP_Query

How to get best selling products using wp_query woocommerce

How to get products list by products ids in woocommerce

How to get all products from current WooCommerce product category?

Get products from a specific brand in WooCommerce using a WP_Query

How to get the products by categories in woocommerce?

WooCommerce search products between price range using WP_Query

WordPress - How to get the category name in WP_Query

WooCommerce cookies and sessions - Get the current products in cart

Excluding duplicates from a foreach loop in a WP_Query for WooCommerce products

How to get up to 100 products in other language (WPML) from WooCommerce using REST API (python wrapper)

How to use WP_Query to get nested values of an object?

Woocommerce how to get products images of recent orders

Exclude current product and featured products from WP_Query

How to get woocommerce products by shipping states

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive