php - Get related product based on subcategory selected from category id with ID 142

Hi I want to display related product in single product page, at the moment it show products that categories selected in

Hi I want to display related product in single product page, at the moment it show products that categories selected in current product. I just want related product have related based on subcategory selected in category with ID 142.

category with ID 142 has name "artists" then artists have subcategory "mike" and "joe". All I want is when product has artist Mike, the related product will show all product that the artists from Mike.

here my code

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$cats_array = array(0);
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
//Select only the category which doesn't have any children
foreach ( $terms as $term ) {
    $children = get_term_children( $term->term_id, 'product_cat' );
    if ( !sizeof( $children ) )
        $cats_array[] = $term->term_id;
}

// Dont bother if none are set
if ( sizeof( $cats_array ) == 1 ) return array();
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__not_in' => array( $product->get_id() ),
'tax_query' => array(
    array(
        'taxonomy' => 'product_cat',
        'field' => 'id',
        'terms' => $cats_array
    ),
)
));
$products                    = new WP_Query( $args );
$woocommerce_loop['name']    = 'related';
$woocommerce_loop['columns'] = apply_filters(
'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>

<div class="related products">

    <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>

    <?php woocommerce_product_loop_start(); ?>

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

            <?php wc_get_template_part('content', 'product-related'); ?>

        <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>

</div>
<?php endif;
wp_reset_postdata();

Please help I'm beginner. thanks

Hi I want to display related product in single product page, at the moment it show products that categories selected in current product. I just want related product have related based on subcategory selected in category with ID 142.

category with ID 142 has name "artists" then artists have subcategory "mike" and "joe". All I want is when product has artist Mike, the related product will show all product that the artists from Mike.

here my code

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product, $woocommerce_loop;
if ( empty( $product ) || ! $product->exists() ) {
return;
}
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$cats_array = array(0);
// get categories
$terms = wp_get_post_terms( $product->id, 'product_cat' );
//Select only the category which doesn't have any children
foreach ( $terms as $term ) {
    $children = get_term_children( $term->term_id, 'product_cat' );
    if ( !sizeof( $children ) )
        $cats_array[] = $term->term_id;
}

// Dont bother if none are set
if ( sizeof( $cats_array ) == 1 ) return array();
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__not_in' => array( $product->get_id() ),
'tax_query' => array(
    array(
        'taxonomy' => 'product_cat',
        'field' => 'id',
        'terms' => $cats_array
    ),
)
));
$products                    = new WP_Query( $args );
$woocommerce_loop['name']    = 'related';
$woocommerce_loop['columns'] = apply_filters(
'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>

<div class="related products">

    <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>

    <?php woocommerce_product_loop_start(); ?>

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

            <?php wc_get_template_part('content', 'product-related'); ?>

        <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>

</div>
<?php endif;
wp_reset_postdata();

Please help I'm beginner. thanks

Share Improve this question edited Jun 21, 2019 at 19:33 Wilda Sagita asked Jun 21, 2019 at 14:32 Wilda SagitaWilda Sagita 34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

you can use this code or adapted to work for you case,, hope its work.. Add it to your function.php

add_filter( 'woocommerce_product_related_posts', 'woocommerce_get_direct_related_products' );

function woocommerce_get_direct_related_products() {
    global $woocommerce, $product;

    // Related products are found from category
    $cats_array = array(0);

    // Get categories
    $terms = wp_get_post_terms( $product->id, 'product_cat' );

    //Select only the category which doesn't have any children
    foreach ( $terms as $term ) {
        $children = get_term_children( $term->term_id, 'product_cat' );
        if ( !sizeof( $children ) )
            $cats_array[] = $term->term_id;
    }

    // Don't bother if none are set
    if ( sizeof( $cats_array ) == 1 ) return array();

    // Meta query
    $meta_query = array();
    $meta_query[] = $woocommerce->query->visibility_meta_query();
    $meta_query[] = $woocommerce->query->stock_status_meta_query();

    $limit = 5;

    // Get the posts
    return array(
        'orderby'       => 'rand',
        'posts_per_page'=> $limit,
        'post_type'     => 'product',
        'fields'        => 'ids',
        'meta_query'    => $meta_query,
        'tax_query'     => array(
            array(
                'taxonomy'  => 'product_cat',
                'field'     => 'id',
                'terms'     => $cats_array
            )
        )
    );
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745380301a4625192.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信