mobile : 09359556950 telegram : @karimeh_net whatsapp : 09359556950

لیست محصولات مشابه در ووکامرس بر اساس دسته بندی

دریافت و نمایش لیست مصحولات بر اساس آخرین ساب کتگوری در یک وب سایت ووکامرسی

 

add_filter( 'woocommerce_related_products', 'woocommerce_get_direct_related_products' );
function woocommerce_get_direct_related_products($args) {
    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;
    }
//print_r( $cats_array);

$cats_k = end($cats_array); // Returns latest value in $array, setting the internal pointer to the last element 
//print_r($cats_k );

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


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

    $limit = -1;

    $post_ids = get_posts(array(
        'orderby'       => 'rand',
        'posts_per_page'=> $limit,
        'post_type'     => 'product',
       // 'fields'        => 'ids',
       // 'meta_query'    => $meta_query,
        'post__not_in' => array( $product->get_id() ),  //this is the way i remove current prod xd
        'tax_query' => array(
            array(
                'taxonomy'  => 'product_cat',
                'field'     => 'id',
                'terms'     => $cats_k,
                            ),
        ),
    ));


$args=array();
    foreach ( $post_ids as $postk ){
    $kid=$postk->ID;
    $args[]=$kid;     
}

     return $args;
reset($cats_k); // Don't forget to reset the pointer!!!
}


 

محصولات مرتبط