WooCommerce: Create a shortcode to display product categories

Remi | September 24, 2013 | in WooCommerce

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.

The author

About the author Remi: Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!

16 Comments

  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. Pingback: Weekly Roundup: November 8, 2013 - Sell With WP

  3. 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!

  4. 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

  5. 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.

  6. 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

Leave a reply