Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce custom product categories hierarchical menu code WordPress

  • SOLVED

I am building a car parts woocoomerce site. I have registered several custom product categories like the example code showing car makers and car models custom product category:

function bss_custom_taxo_makes() {
$single = 'Make'; $plural = 'Makes';
$labels = array(
'name' => $single,
'singular_name' => $single,
'menu_name' => '- '.$plural,
'all_items' => 'All '.$plural,
// 'parent_item' => 'Parent '.$single,
// 'parent_item_colon' => 'Parent '.$single.':',
'new_item_name' => 'New '.$single,
'add_new_item' => 'Add New '.$single,
'edit_item' => 'Edit '.$single,
'update_item' => 'Update '.$single,
'separate_items_with_commas' => 'Separate '.$plural.' with commas',
'search_items' => 'Search '.$plural,
'add_or_remove_items' => 'Add or remove '.$plural,
'choose_from_most_used' => 'Choose from the most used '.$plural
);
$args = array(
'labels' => $labels,
'hierarchical' => FALSE,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'bss_prod_makes', 'product', $args );
}

function bss_custom_taxo_models() {
$single = 'Model'; $plural = 'Models';
$labels = array(
'name' => $single,
'singular_name' => $single,
'menu_name' => '- '.$plural,
'all_items' => 'All '.$plural,
// 'parent_item' => 'Parent '.$single,
// 'parent_item_colon' => 'Parent '.$single.':',
'new_item_name' => 'New '.$single,
'add_new_item' => 'Add New '.$single,
'edit_item' => 'Edit '.$single,
'update_item' => 'Update '.$single,
'separate_items_with_commas' => 'Separate '.$plural.' with commas',
'search_items' => 'Search '.$plural,
'add_or_remove_items' => 'Add or remove '.$plural,
'choose_from_most_used' => 'Choose from the most used '.$plural
);
$args = array(
'labels' => $labels,
'hierarchical' => FALSE,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'bss_prod_models', 'product', $args );
}



My issue is this: I wish to display a list menu that is hierarchical. For Example on the main shop page i would show a list of makers like so :

- Audi
- BMW
- Fiat
- Ford

If the visitor clicked Fiat for example, then i would want to show subcategories based upon this logic:
All models custom product type (bss_prod_models) where the makers custom product type (bss_prod_makes) is equal to 'Fiat"

- Audi
- BMW
- Fiat
- - Brava
- - Bravo
- - Coupe
- Ford

Answers (4)

2019-02-19

Bob answers:

I have created similar thing here.

Do you want it like this?

https://rmdmotors.com/bikes-for-sale/


User180917 comments:

That looks similar to what I am trying to achieve - is it using custom product types ?


Bob comments:

Can you ping me on skype at thevaghela ?

Or email me at [email protected]

I will share complete code with you.

2019-02-18

Arnav Joy answers:

You can use following function

https://developer.wordpress.org/reference/functions/get_terms/

And to show child you can use parent argument as shown here

https://developer.wordpress.org/reference/classes/wp_term_query/__construct/


Arnav Joy comments:

You can also check this

https://codex.wordpress.org/Function_Reference/_get_term_hierarchy


User180917 comments:

thanks but the custom prod categories are not hierarchical themselves i.e. they have no direct relationship, I need dynamically relate the models belonging to a car make by finding all the products with the specific car make and parsing all the models that belong to that make if you see what I mean


User180917 comments:

also if the resulting code is too intensive (its a large product catalog) I would pre-cache the results of this to avoid overloading the site.


Arnav Joy comments:

can you show screenshot of the admin to understand better how you have configured it?
or if it is ok, you can provide me access so I can check my self
my email is: [email protected]

2019-02-18

Hugo Gonçalves answers:

Hi User180917!

"thanks but the custom prod categories are not hierarchical themselves i.e. they have no direct relationship, I need dynamically relate the models belonging to a car make by finding all the products with the specific car make and parsing all the models that belong to that make if you see what I mean"

Do you have to create those categories non-hierarchically?
I mean, couldn't you create sub-categories already that are have car models under each specific maker?

Cheers
Hugo


User180917 comments:

I'm not using categories - I'm using custom product types

2019-02-19

Cesar Contreras answers:

Have you solved this question?