Filter WooCommerce related products by Polylang language



PHP Snippet 1:

add_filter( 'woocommerce_product_related_posts', 'custom_related_products' );
function custom_related_products($product){
    global $woocommerce;
    // Meta query
    $meta_query = array();
    $meta_query[] = $woocommerce->query->visibility_meta_query();
    $meta_query[] = $woocommerce->query->stock_status_meta_query();
    $meta_query   = array_filter( $meta_query );
    // Get the posts
    $related_posts = get_posts( array(
            'orderby'        => 'rand',
            'posts_per_page' => '4',
            'post_type'      => 'product',
            'fields'         => 'ids',
            'meta_query'     => $meta_query,
            'suppress_filters' => false
        ) );

        if ( $related_posts->have_posts() ) {
          while ( $related_posts->have_posts() ) : $related_posts->the_post();
            if(pll_get_post_language(get_the_ID())){
               //Not sure its the right approach for this..
            }
          endwhile;
        }
    $related_posts = array_diff( $related_posts, array( $product->id ), $product->get_upsells() );
    return $related_posts;
}

PHP Snippet 2:

$related_posts = get_posts( array(
    'orderby'        => 'rand',
    'posts_per_page' => '4',
    'post_type'      => 'product',
    'meta_query'     => $meta_query,
    'lang'           => 'en'
) );

if ($related_posts) {
    foreach ($related_posts as $post) {
        setup_postdata($post);
        // something like <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    }
}

wp_reset_postdata();

PHP Snippet 3:

function mycustomplugin_posts($params) {
    // get the url params

    $page = $params->get_param('page') ? $params->get_param('page') : 0;
    $per_page = $params->get_param('per_page') ? $params->get_param('per_page') : 10;
    $offset = $params->get_param('offset') ? $params->get_param('offset') : 0;
    $order = $params->get_param('order') ? $params->get_param('order') : 'desc';
    $order_by = $params->get_param('order_by') ? $params->get_param('order_by') : 'date';
    $lang = array_search($params->get_param('lang'),polylang_json_api_languages(), true) ? $params->get_param('lang') : pll_default_language();
    
    $args = [
        'post_type' => 'post',
        'page' => $page,
        'numberposts' => $per_page,
        'offset' => $offset,
        'order' => $order,
        'orderby' => $order_by,
        'tax_query' => [
            [
                'taxonomy'  => 'language',
                'field'     => 'slug',
                'terms'     => $lang
            ]
        ]
    ];

    $posts = get_posts($args);
    $data = [];
    $i = 0;

    foreach($posts as $post) {
        
        // Extract the post data

        $data[$i]['id'] = $post->ID;
        $data[$i]['title'] = $post->post_title;
        $data[$i]['content'] = $post->post_content;
        $data[$i]['excerpt'] = e42_the_short_content($post->post_content, 300);
        $data[$i]['slug'] = $post->post_name;
        $data[$i]['date'] = $post->post_date;
        $data[$i]['link'] = get_permalink($post->ID);
        $data[$i]['author'] = get_the_author_meta('display_name', $post->post_author);
        $data[$i]['categories'] = array();
        $data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
        $data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
        $data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
        
        foreach(get_the_category($post->ID) as $category){
            array_push($data[$i]['categories'],$category->term_id);
        }
        $i++;
    }
    
    // Create the response object

    $response = new WP_REST_Response( $data );

    // Add a custom status code

    $response->set_status( 200 );
    return $response;
}

PHP Snippet 4:

if (pll_current_language()!=pll_get_post_language($post_object->ID)) {
    continue;
}