wc_product_dropdown_categories

WooCommerce Dropdown categories.

Description

(string) wc_product_dropdown_categories( (array) $args = array(), (int) $deprecated_hierarchical = 1, (int) $deprecated_show_uncategorized = 1, (string) $deprecated_orderby = '' ); 

Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258. We use a custom walker, just like WordPress does.

Returns (string)

Parameters (4)

0. $args — Optional. (array) => array()
(default: 1)
1. $deprecated_hierarchical — Optional. (int) => 1
The deprecated hierarchical.
2. $deprecated_show_uncategorized — Optional. (int) => 1
The deprecated show uncategorized.
3. $deprecated_orderby — Optional. (string) => ''
The deprecated orderby.

Usage

  1. if ( !function_exists( 'wc_product_dropdown_categories' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php'; 
  3.  
  4. // (default: 1) 
  5. $args = array(); 
  6.  
  7. // The deprecated hierarchical. 
  8. $deprecated_hierarchical = 1; 
  9.  
  10. // The deprecated show uncategorized. 
  11. $deprecated_show_uncategorized = 1; 
  12.  
  13. // The deprecated orderby. 
  14. $deprecated_orderby = ''; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wc_product_dropdown_categories($args, $deprecated_hierarchical, $deprecated_show_uncategorized, $deprecated_orderby); 
  18.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-term-functions.php  
  1. function wc_product_dropdown_categories( $args = array(), $deprecated_hierarchical = 1, $deprecated_show_uncategorized = 1, $deprecated_orderby = '' ) { 
  2. global $wp_query; 
  3.  
  4. if ( ! is_array( $args ) ) { 
  5. wc_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.' ); 
  6.  
  7. $args['show_count'] = $args; 
  8. $args['hierarchical'] = $deprecated_hierarchical; 
  9. $args['show_uncategorized'] = $deprecated_show_uncategorized; 
  10. $args['orderby'] = $deprecated_orderby; 
  11.  
  12. $current_product_cat = isset( $wp_query->query_vars['product_cat'] ) ? $wp_query->query_vars['product_cat'] : ''; 
  13. $defaults = array( 
  14. 'pad_counts' => 1,  
  15. 'show_count' => 1,  
  16. 'hierarchical' => 1,  
  17. 'hide_empty' => 1,  
  18. 'show_uncategorized' => 1,  
  19. 'orderby' => 'name',  
  20. 'selected' => $current_product_cat,  
  21. 'menu_order' => false,  
  22. ); 
  23.  
  24. $args = wp_parse_args( $args, $defaults ); 
  25.  
  26. if ( 'order' === $args['orderby'] ) { 
  27. $args['menu_order'] = 'asc'; 
  28. $args['orderby'] = 'name'; 
  29.  
  30. $terms = get_terms( 'product_cat', apply_filters( 'wc_product_dropdown_categories_get_terms_args', $args ) ); 
  31.  
  32. if ( empty( $terms ) ) { 
  33. return; 
  34.  
  35. $output = "<select name='product_cat' class='dropdown_product_cat'>"; 
  36. $output .= '<option value="" ' . selected( $current_product_cat, '', false ) . '>' . esc_html__( 'Select a category', woocommerce ) . '</option>'; 
  37. $output .= wc_walk_category_dropdown_tree( $terms, 0, $args ); 
  38. if ( $args['show_uncategorized'] ) { 
  39. $output .= '<option value="0" ' . selected( $current_product_cat, '0', false ) . '>' . esc_html__( 'Uncategorized', woocommerce ) . '</option>'; 
  40. $output .= "</select>"; 
  41.  
  42. echo $output;