dpi74187 2019-03-14 17:03
浏览 77
已采纳

从WooCommerce中的相关产品中排除产品类别

I am trying to exclude a product category (id: 78) from the related products section in WooCommerce. I have a custom query already in place which only shows related products from the child category.

This is my code:

<?php global $post, $product;

if (empty($product) || !$product->exists()) {
    return;
}

$subcategories_array = array(0);
$all_categories = wp_get_post_terms($product->id, 'product_cat');
foreach ($all_categories as $category) {
    $children = get_term_children($category->term_id, 'product_cat');
    if (!sizeof($children)) {
    $subcategories_array[] = $category->term_id;
    }
}

if (sizeof($subcategories_array) == 1) {
    return array();
}

$args = array(
    'orderby' => 'rand',
    'posts_per_page' => 3,
    'post_type' => 'product',
    'category__not_in' => array( 78 ),
    'fields' => 'ids',
    'meta_query' => $meta_query,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $subcategories_array,
        )
    )
);

$wp_query = new WP_Query($args);

if ($wp_query->have_posts()):
    ?>

    <section class="related products">

    <h2><?php esc_html_e('Related products', 'woocommerce'); ?></h2>

    <?php woocommerce_product_loop_start(); ?>

    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

        <?php
        global $post, $product;

        $post_object = get_post($product->get_id());

        setup_postdata($GLOBALS['post'] = & $post_object);

        wc_get_template_part('content', 'product');
        ?>

    <?php endwhile; ?>

    <?php woocommerce_product_loop_end(); ?>

    </section>

    <?php
endif;

wp_reset_postdata();

However excluding the aforementioned product category doesn't seem to be working as I expected.

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dsy19890123 2019-03-14 18:21
    关注

    Woocommerce product categories is a custom taxonomy and can't work as Wordpress categories in a WP_Query using category__not_in … Instead try to combine that in the existing tax_query.

    Also on your existing tax_query, for fields argument you need to replace id by term_id (which is enabled by default, so you can remove the line)
    see WP_Query Taxonomy Parameters official documentation

    Try the following:

    $args = array(
        'orderby' => 'rand',
        'posts_per_page' => 3,
        'post_type' => 'product',
        'fields' => 'ids',
        'meta_query' => $meta_query,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'terms'    => $subcategories_array,
            ),
            array(
                'taxonomy' => 'product_cat',
                'terms'    => array( 78 ),
                'operator' => 'NOT IN',
            )
        )
    );
    

    It should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R