1

I have a wordpress site with woocommerce and polylang plugins. I need to filter products by category. For the default language, everything works correctly. But products are not displayed in another language.

Each language has a unique set of products in the corresponding language

In a non-default language, even the check for the presence of posts does not pass

$categories = $_POST['categories'];
    $categories_lang = $_POST['language'];

    $product_category_slug_aa;
    $product_category_slug_employees;
    if ($categories_lang == 'ru') {
        $product_category_slug_aa = "course_base";
        $product_category_slug_employees = "employees";
    } else if ($categories_lang == 'en'){
        $product_category_slug_aa = "course_base";
        $product_category_slug_employees = "employees";
    } else if ($categories_lang == 'nb'){
        $product_category_slug_aa = "course_base-nb";
        $product_category_slug_employees = "employees-nb";
    }

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 20,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $categories_lang,
            ),
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $product_category_slug_aa,
                'operator' => 'NOT IN',
            ),
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $product_category_slug_employees,
                'operator' => 'NOT IN',
            ),
        ),
    );
        
    if (!empty($categories)) {
        $tax_queries = array();
        foreach ($categories as $category) {
            $tax_queries[] = array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $category,
            );
        }
        $args['tax_query'] = array_merge($args['tax_query'], $tax_queries);
    }

    $query = new WP_Query($args);

    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            $terms = get_the_terms(get_the_ID(), 'product_cat');
            if ($terms && !is_wp_error($terms)) {
                $courses_found = false;
                foreach ($terms as $term) {
                    if ($term->slug === 'courses' || $term->slug === 'courses-nb') {
                        $courses_found = true;
                    }
                }
                if ($courses_found) {
                    wc_get_template_part('content', 'product-courses');
                }
            }
        }
    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.