1
votes

I have found a WooCommerce code snippet that adds sorting on Category pages, and it works.

But the problem is that the sorting subcategory is only displayed on the Parent category page, but not in the subcategories pages.

  • This is the category page:

    Categories page

  • The problem is that the sorting is not working on subcategory pages:

    Sub category page

What do I need to change in my code to achieve that?

Here is my code:

function tutsplus_product_subcategories( $args = array()) {
$parentid = get_queried_object_id(); 
$args = array(
    'parent' => $parentid
);

$terms = get_terms( 'product_cat', $args );

if ( $terms) {

    echo '<ul class="wooc_sclist">';

        foreach ( $terms as $term ) {

            echo '<li class="category">';                 


                echo '<h2>';
                    echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
                        echo $term->name;
                    echo '</a>';
                echo '</h2>';

            echo '</li>';


        }

        echo '</ul>';
    }
}
add_action( 'woocommerce_before_shop_loop', 'tutsplus_product_subcategories', 50 );

Reference: Display WooCommerce Categories, Subcategories, and Products in Separate Lists

1
sorry for the delay,here are some screenshots,this is the sub category sorting i made in my parent category page: prntscr.com/blpxtq this is the sub category page,the problem is that the sorting is not showing in it:prntscr.com/blpy48ssabin
It would be helpful to see more debug results. What ends up getting stored into $terms on the subcategory page? Is it what you expect? And is the if statement condition returning true so that it attempts to create the unordered list?PapaHotelPapa
i did a little debugging. the problem is that i am not able to get the parent category of the subcategory id,when i'm using get_queried_object_id(); it gives me the current category,how can i make it give me the id of the sub category parent category?ssabin
i just edited and added the conclusionsssabin
It doesnt change the child id.ssabin

1 Answers

-1
votes

Well i found out how to make it so i will share it with you:

        function sub_fillter(){
        $sub = wp_get_post_terms(get_the_ID(),'product_cat');
        if ( is_subcategory()){

            $cat = array_shift( $sub );
            $cat=get_term_top_most_parent($cat->term_id,'product_cat'); 
            tutsplus_product_subcategories($cat);
        }
    }
        add_action('woocommerce_before_shop_loop', 'sub_fillter', 50);

function is_subcategory (){
    $cat = get_query_var('cat');
    $category = get_category($cat);
    $category_gt_parent="";
    return ( $category_gt_parent == '0' ) ? false : true;
}

function tutsplus_product_subcategories( $cat) {
$parentid = $cat->term_id; 
$args = array(
    'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );

if ( $terms || is_subcategory()) {

    echo '<ul class="wooc_sclist">';

        foreach ( $terms as $term ) {

            echo '<li class="category">';                 

                echo '<h2>';
                    echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
                        echo $term->name;
                    echo '</a>';
                echo '</h2>';

            echo '</li>';


        }

        echo '</ul>';
    }
}