I do not wish to use any plugin to complete this task as I've experienced conflicts recently with a similar project that broke a site. So I wish to create this functionality from basics.
I require a dropdown list on the product category pages to select products by Brand. The dropdown list would show all brands. When you select one the site displays only those products assigned to the brand. We do not need to use the built-in dropdown that allows viewing by newness, price, popularity etc.
Using the WooCommerce 'Brands
' taxonomy I have set up my brands and allocated each product a brand.
I can view the array of all brands and their attributes with the following code:
$brands = get_terms('brand');
print_r($brands);
Which outputs the following:
Array (
[0] => WP_Term Object ( [term_id] => 978 [name] => Imari Sometsuke [slug] => imari-sometsuke [term_group] => 0 [term_taxonomy_id] => 978 [taxonomy] => brand [description] => [parent] => 0 [count] => 1 [filter] => raw )
[1] => WP_Term Object ( [term_id] => 982 [name] => Kutani [slug] => kutani [term_group] => 0 [term_taxonomy_id] => 982 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[2] => WP_Term Object ( [term_id] => 977 [name] => Kutani Shoza [slug] => kutani-shoza [term_group] => 0 [term_taxonomy_id] => 977 [taxonomy] => brand [description] => [parent] => 0 [count] => 4 [filter] => raw )
[3] => WP_Term Object ( [term_id] => 979 [name] => Kutani Tokkuri [slug] => kutani-tokkuri [term_group] => 0 [term_taxonomy_id] => 979 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[5] => WP_Term Object ( [term_id] => 985 [name] => Nishikawa Sukenobu [slug] => nishikawa-sukenobu [term_group] => 0 [term_taxonomy_id] => 985 [taxonomy] => brand [description] => [parent] => 0 [count] => 1 [filter] => raw )
[6] => WP_Term Object ( [term_id] => 984 [name] => Shinsui Ito [slug] => shinsui-ito [term_group] => 0 [term_taxonomy_id] => 984 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[7] => WP_Term Object ( [term_id] => 976 [name] => Takeji Asano [slug] => takeji-asano [term_group] => 0 [term_taxonomy_id] => 976 [taxonomy] => brand [description] => [parent] => 0 [count] => 2 [filter] => raw )
[8] => WP_Term Object ( [term_id] => 980 [name] => Toshusai Sharaku [slug] => toshusai-sharaku [term_group] => 0 [term_taxonomy_id] => 980 [taxonomy] => brand [description] => [parent] => 0 [count] => 3 [filter] => raw )
)
How would one go about constructing the dropdown (select) list to create this? I imagine it would have the framework something like, which I have started:
<?php
$brands = get_terms('brand');
//print_r($brands);
?>
<select name="orderby" class="orderby">
<?php foreach ( $brands as ??? ) : ?>
<option value="<?php echo esc_attr( $??? ); ?>" <?php selected( $orderby, $??? ); ?>><?php echo esc_html( $??? ); ?></option>
<?php endforeach; ?>
</select>