• I have been trying to style my product category list but to do so means I need to add a some mark up to them.

    Ok just to sum up what I want to achieve seeing as I have been doing this for a week and maybe on completely the wrong path.

    I want to add classes to current category and current category ancestors
    I would also like to add mark up to the < a> tags as well. I have been reading about “walkers” all week and still got almost no clue.

    I a using woo commerce and I can get it working by adding the mark up to
    class-product-cat-list-walker.php

    public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
    		$output .= '<li class="cat-item cat-item-' . $cat->term_id;
    
    		if ( $args['current_category'] == $cat->term_id ) {
    			$output .= ' current-cat';
    		}
    
    		if ( $args['has_children'] && $args['hierarchical'] ) {
    			$output .= ' cat-parent';
    		}
    
    		if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat->term_id, $args['current_category_ancestors'] ) ) {
    			$output .= ' current-cat-parent';
    		}
    
    		$output .=  '"><a href="' . get_term_link( (int) $cat->term_id, 'product_cat' ) . '" class="btn btn--small  btn--full  catagory-menue__btn"  >'  . __( $cat->name, 'woocommerce' ) .  '</a>';
    
    		if ( $args['show_count'] ) {
    			$output .= ' <span class="count">(' . $cat->count . ')</span>';
    		}
    	}

    I added my mark up and it works. But it is not ideal as then I cannot update easily . I tried copying the code for the whole walker to my functions.php file on my theme and changed

    class WC_Product_Cat_List_Walker extends Walker
    to
    class Book_Cat_Btn extends Walker
    to keep it form getting confused. This worked! which was amazing till I realised I no longer have some of the classes that woo commerce had added to my category list.

    it seems to me that it all works apart from

    if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat->term_id, $args['current_category_ancestors'] ) ) {
    			$output .= ' current-cat-parent';
    		}

    Is there any way I can get this to work? I assumed current_category_ancestors was part of WordPress code but it seems not and I have no clue how to replicate what ever is making it work.


    It looks like this may do what I want but I am having trouble working it out

    thanks for any help anyone can give me. Even a online lesson or a book i can get that would tell me what is going on, I just completed code academy PHP and it helped but i am still at the stage where I don’t even know what to ask most of the time

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t know much of WooCommerce, but it sounds like your issue is how to get an array of category ancestors, which I can help you with. You could work out something with wp_list_categories() but it is set up to output HTML, while what you want are arrays, and converting adds more complexity than is necessary.

    Another issue is WP functions are more oriented for collecting category children, not parents and ancestors. As it happens though, collecting parent IDs is not that complicated. Even so, it’d be worth looking through Woo code to find the function that populates ‘current_category_ancestors’. (There is no such WP core function) No point rewriting what has already been written.

    If you can’t find anything, the current category’s parent ID is available in the category object. You can then use get_category() to get the parent object, which will then have its parent’s ID. You can climb up through the generations until you find ID==0, which will be the top level category.

    Thread Starter jasperg

    (@jasperg)

    @bcworkz

    Thanks so much for your help. I still have not got it to work, but following your advice I did get a better understanding of what was going on, and it seems what I wanted was more complicated than it was worth for the time being. It will have to be put on a list of things to do at a latter date. (when I am better at php)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘adding mark up to woo commerce product categories’ is closed to new replies.