Woocommerce 中的商品不会以非默认语言的 AJAX 请求显示

问题描述 投票:0回答:0

我有一个带有 woocommerce 和 polylang 插件的 wordpress 网站。我需要按类别过滤产品。对于默认语言,一切正常。但是产品不会以另一种语言显示。

每种语言都有相应语言的独特产品集

在非默认语言中,即使检查是否存在帖子也不会通过

$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');
                }
            }
        }
    }
php ajax wordpress woocommerce polylang
© www.soinside.com 2019 - 2024. All rights reserved.