WooCommerce: Create a shortcode to display product categories

Here is a quick snippet that will allow you to create a dropdown select that displays the WooCommerce product categories. Once the dropdown is clicked, your visitors will be automatically redirected to the product category page! Place this code in the functions.php file within your theme folder.

16 responses to “WooCommerce: Create a shortcode to display product categories”

  1. Dan

    Hi. Very useful, thanks! I’ve tried customising this using “child_of” to no success. I’m trying to build a product finder so, for example, user can select “car manufacturer” from a drop down which then populates a second drop down with a list of “car models”.
    So if they select FORD from car manufacturer the second drop down will list the sub-cats of FORD, such as Focus, S-Max, etc. Any ideas wise one?

    1. rezwan

      Dan

      October 11, 2013 at 1:26 pm

      Hi. Very useful, thanks! I’ve tried customising this using “child_of” to no success. I’m trying to build a product finder so, for example, user can select “car manufacturer” from a drop down which then populates a second drop down with a list of “car models”.
      So if they select FORD from car manufacturer the second drop down will list the sub-cats of FORD, such as Focus, S-Max, etc. Any ideas wise one?

      /**************************************************************/

      dan have you sorrted this problem?? I am searching for solution as well

    2. stavroch

      I want also to make the same issue. Can you share the steps that you build the above structure?

  2. […] some WooCommerce hints? Remi Corson posted a neat tutorial on creating a shortcode to display product categories, and Patrick Rauland wrote a how-to for removing free shipping for certain […]

  3. jansolo76

    I’m also searching for such a solution like the one described above.
    Maybe Remi can help finding one (please?)?

  4. TopdiR

    Hello!

    Thank you for a very useful shortcode. Works perfectly. I using it on the Category page.

    Tell me please, can you modify it to appear only subcategory of the current category, and not all categories with subcategories?

    Thanks!

  5. yoursrazi

    Categories in the dropdown are alphabetically ordered. However, you can drag and drop the woocommerce product categories in WP backend to reorder them as you want. I was wondering if the categories can be forced to appear in the dropdown as they are reordered in WP backend. Thanks

    1. please post your comments on the right post pages, and don’t duplicate them on many posts thanks. I would be ashamed to have to ban you! ;-)

      1. yoursrazi

        Apologies, could you please also answer my question. Thanks

        1. I don’t do WooCommerce support on my site sorry.

  6. watchteller

    Great…..

    Worked…..

    Appreciated if your any snippets (to combine use exclude category on product category widget snippets) to replace “Select a category” text to any category in your next post.

    1. watchteller

      I had tried this but no luck.
      if(!is_admin() && is_page(xxxxx) && is_active_widget(xxxxxx)){
      function art_architecture_textbooks( $cat_args ) {
      $cat_args['show_option_none'] = __('Art & Architecture');
      return $cat_args;
      }}
      add_filter( 'woocommerce_product_categories_widget_args', 'art_architecture_textbooks' );

      and

      if(!is_admin() && is_page(xxxxx) && is_active_widget(xxxxxx)){
      function art_architecture_textbooks( $list_args ) {
      $list_args['show_option_none'] = __('Art & Architecture');
      return $list_args;
      }}
      add_filter( 'woocommerce_product_categories_widget_args', 'art_architecture_textbooks' );

      I am stuck here http://awesomescreenshot.com/0f436fpw41 (responsive view). Look forward your light.

  7. wpnoob

    Is it possible to generate a dropdowns as the hierarchy goes and search by selected combination?

  8. jamesdowen

    I’ve done all that and the dropdown shows. However when I select a category, nothing happens.

    Can you please advise?

  9. tellstevens

    Hi Remi,

    I used your snippet on a website a little over a year ago and everything worked seamlessly. I recently did an upgrade on a theme and I added this snippet back to my functions file but I’m not even seeing a dropdown. When I inspect the file I can see some warnings.

    Warning – *****/wp-content/plugins/woocommerce/includes/wc-term-functions.php on line 114
    Warning – *****/wp-content/plugins/woocommerce/includes/wc-term-functions.php on line 115
    Warning – *****/wp-content/plugins/woocommerce/includes/wc-term-functions.php on line 116
    Warning – *****/wp-content/plugins/woocommerce/includes/wc-term-functions.php on line 117

    I looked into that functions file and it has to do w/ WC dropdowns.

    /**
    * WooCommerce Dropdown categories
    *
    * Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
    * We use a custom walker, just like WordPress does
    *
    * @param int $deprecated_show_uncategorized (default: 1)
    * @return string
    */
    function wc_product_dropdown_categories( $args = array(), $deprecated_hierarchical = 1, $deprecated_show_uncategorized = 1, $deprecated_orderby = ” ) {
    global $wp_query;

    if ( ! is_array( $args ) ) {
    _deprecated_argument( ‘wc_product_dropdown_categories()’, ‘2.1’, ‘show_counts, hierarchical, show_uncategorized and orderby arguments are invalid – pass a single array of values instead.’ );

    $args[‘show_counts’] = $args;
    $args[‘hierarchical’] = $deprecated_hierarchical;
    $args[‘show_uncategorized’] = $deprecated_show_uncategorized;
    $args[‘orderby’] = $deprecated_orderby;
    }

    Can you please advise?

    Thank you

  10. cliotine

    Hello Remi,
    Thanks a lot for you code!
    I am trying to use it on my website: http://blog.institutmichelguerard.com/formation/programmes/ but when I chose a category, nothing happens. Could you help me?
    And I would like to ‘hide’ some categories in the dropdown, could you explain me how I can do it?
    Thanks a lot for your help!
    Regards,
    Caroline

Leave a Reply