I want to add a custom dropdown list of an attribute (in this case it’s the brand), with the options lead to the attribute page as a link.
I got this working
add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
function wc_reg_for_menus() {
$terms = get_terms( 'pa_marke' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select>';
foreach ( $terms as $term ) {
echo '<option value="'.$term->name.'">'.$term->name.'</option>';
}
echo '</select>';
}
}
And I think I somehow need to add this part
get_term_link( WP_Term|int|string $term, string $taxonomy = '' )
Thank you!
Felix
2
Answers
Thank you!
Just now I found a solution without jQuery, that works quite fine as well:
Only after the new page loaded, the first option of the menu is selected again, but that´s ok for me, because it won't be displayed there later on.
Your code works well for getting values for a specific taxonomy (attribute).
I only changed the
get_terms
function.I also added some data attributes to get the taxonomy and slug of each term (option).
Then you will need to use a jQuery script to redirect the user based on the chosen attribute option.
The code has been tested and works. Add it to your active theme’s functions.php.