List of WooCommerce brand taxonomy terms in the drop-down list

I don’t want to use any plugin to accomplish this task, as I recently ran into conflicts with a similar project that broke the site. Therefore, I want to create this functionality from the basics.

I need a drop-down list on the product category pages to select products by brand. All brands will be shown in the drop-down list. When you select one, only those products that are assigned to the brand are displayed on the site. We do not need to use the built-in drop-down list, which allows you to browse by novelty, price, popularity, etc.

Using the WoolCommerce taxonomy , I created my own brands and allocated a brand to each product . 'Brands

I can view an 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 [] = > [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 [] = > [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 [] = > [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 [] = > [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 [] = > [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 [] = > [description] = > [parent] = > 0 [count] = > 3 [filter] = > raw)

)

()? , - , :

<?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>

+4
1

term (WP_Term Object), :

<?php

    $brands = get_terms( 'brand', array(
        'orderby'  => 'name'  // orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description')
    ) );
    //print_r($brands);

?>
<select name="orderby" class="orderby">
    <?php
        foreach ( $brands as $key => $brand ) :
            $brand_id = $brand->term_id;
            $brand_name = $brand->name;
            $brand_slug = $brand->slug;
            $brand_term_group = $brand->term_group;
            $brand_term_taxonomy = $brand->term_taxonomy_id;
            $brand_taxonomy = $brand->taxonomy;
            $brand_description = $brand->description;
            $brand_parent = $brand->parent;
            $brand_count = $brand->count;
            $brand_filter = $brand->filter;

            $number = $key+1;
            $option = 'option-' . $number;
    ?>
        <option value="<?php echo $option; ?>"><?php echo $brand->name; ?></option>
    <?php endforeach; ?>
</select>

+2

Source: https://habr.com/ru/post/1652116/


All Articles