How to add a new filtering dropdown by product tags - Woocommerce

347 Views Asked by At

Hope everyone is doing well. I have a question that I need help with and I would appreciate any guidance.

I want to add a new dropdown filter system that will list all product tags in the shop and someone can sort by the specific tag. Please see the image to get a clear understanding.

The position of the custom_catalog_ordering will be beside the default one.

enter image description here

// Add custom woocommerce ordering


add_action( 'woocommerce_before_shop_loop', 'custom_catalog_ordering', 29 ); 


function custom_catalog_ordering() { 
    global $wp_query; 
 
    if ( 1 === (int) $wp_query->found_posts || ! woocommerce_products_will_display() ) { 
        return; 
    } 
 
    $orderby = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); 
    $show_default_orderby = 'menu_order' === apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); 


    
    $catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array( 
        'menu_order' => __( 'Default sorting', 'woocommerce' ),  
        'popularity' => __( 'Sort by popularity', 'woocommerce' ),  
        'rating' => __( 'Sort by average rating', 'woocommerce' ),  
        'date' => __( 'Sort by newness', 'woocommerce' ),  
        'price' => __( 'Sort by price: low to high', 'woocommerce' ),  
        'price-desc' => __( 'Sort by price: high to low', 'woocommerce' ),  
 ) ); 
 
    if ( ! $show_default_orderby ) { 
        unset( $catalog_orderby_options['menu_order'] ); 
    } 
 
    if ( 'no' === get_option( 'woocommerce_enable_review_rating' ) ) { 
        unset( $catalog_orderby_options['rating'] ); 
    } 
 
    wc_get_template( 'loop/orderby.php', array( 'catalog_orderby_options' => $catalog_orderby_options, 'orderby' => $orderby, 'show_default_orderby' => $show_default_orderby ) ); 
} 

I want to modify this code to show a dropdown of all product tags in the shop.

0

There are 0 best solutions below