Woocommerce Сопутствующий товар по таксономии - PullRequest
0 голосов
/ 27 июня 2018

Мне нужно изменить связанный продукт по таксономии, и если таксономии не хватает продукта, отобразите остаток от другой таксономии.

Я имею 2 таксономии; product_tag и product_cat. Иногда продуктов в product_tag не больше 4, поэтому мне нужно использовать другую таксономию для выполнения 4 продуктов в связанном продукте.

Так что мне нужно 4 связанных продукта, если product_tag имеет их все, так что ничего, если не использовать product_cat для завершения 4 продукта.

Любая помощь будет полезна.

<?php
        // get the custom post type's taxonomy terms
        $related_category = wp_get_object_terms( $post->ID, 'product_cat', array('fields' => 'ids') );
        $related_tag = wp_get_object_terms( $post->ID, 'product_tag', array('fields' => 'ids') );
        // arguments
        $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 4, // you may edit this number
        'orderby' => 'rand',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_tag',
                'field' => 'id',
                'terms' => $related_tag
            )
        ),
        'post__not_in' => array ($post->ID),
        );
        $related_items = new WP_Query( $args );
        // loop over query
        if ($related_items->have_posts()) :
        while ( $related_items->have_posts() ) : $related_items->the_post();
        ?>
        <div class="related_item">
          <div class="Related_image">
            <a href="<?php the_permalink() ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
          </div>
            <div class="Related_title">
              <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><h2>

            </div>
          </div>
        <?php
        endwhile;
        endif;
        // Reset Post Data
        wp_reset_postdata();
        ?>

1 Ответ

0 голосов
/ 28 июня 2018

Я думаю, что сделал это. поэтому, если этот код неверен или может быть упрощен, просто исправьте его. Я хочу 5 результатов продукта. Таким образом, все это должно быть от product_tag, если оно доступно. если не использовать product_cat. поэтому я объединил 2 аргумента здесь:

<?php
        // get the custom post type's taxonomy terms
        $related_tag = wp_get_object_terms( $post->ID, 'product_tag', array('fields' => 'ids'));
        $related_cat = wp_get_object_terms( $post->ID, 'product_cat', array('fields' => 'ids'));


        // arguments 1
        $args1 = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 5,
        'orderby' => '',

        'tax_query' => array(

            array(

                'taxonomy'     => 'product_tag',
                'field'        => 'id',
                'terms'        =>  $related_tag,
            ),

        ),
        'post__not_in' => array ($post->ID),
         );
        // arguments 2
        $args2 = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'posts_per_page' => 5,
        'orderby' => 'rand',

        'tax_query' => array(

            array(

                'taxonomy'     => 'product_cat',
                'field'        => 'id',
                'terms'        =>  $related_cat,
            ),

        ),
        'post__not_in' => array ($post->ID),

        );

        $related_items1 = new WP_Query( $args1 );
        $related_items2 = new WP_Query( $args2 );
        $related_items = new WP_Query();
        $related_items->posts = array_merge( $related_items1->posts, $related_items2->posts );

        $related_items3->post_count=5 - $related_items1->post_count;

        $related_items->post_count = $related_items1->post_count + $related_items3->post_count;


        // loop over query
        if ($related_items->have_posts()) :
        while ( $related_items->have_posts() ) : $related_items->the_post();
        ?>
        <div class="related_item">
          <div class="Related_image">
            <a href="<?php the_permalink() ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
          </div>
            <div class="Related_title">
              <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><h2>

            </div>
          </div>
        <?php
        endwhile;
        endif;
        // Reset Post Data
        wp_reset_postdata();
        ?>
...