aiwoo_shortcode_productcat

The WooCommerce Menu Extension aiwoo shortcode productcat function.

Description

aiwoo_shortcode_productcat( $atts, (null) $content = null ); 

Parameters (2)

0. $atts
The atts.
1. $content — Optional. (null) => null
The content.

Usage

  1. if ( !function_exists( 'aiwoo_shortcode_productcat' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-menu-extension/include/frontend.inc.php'; 
  3.  
  4. // The atts. 
  5. $atts = null; 
  6.  
  7. // The content. 
  8. $content = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = aiwoo_shortcode_productcat($atts, $content); 
  12.  

Defined (1)

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

/include/frontend.inc.php  
  1. function aiwoo_shortcode_productcat( $atts, $content = null ) { 
  2. global $woocommerce, $wp_query, $post;  
  3. $atts = shortcode_atts(array( 
  4. "show_count" => 0,  
  5. "hierarchical" => 0,  
  6. "show_children_only" => 0,  
  7. "dropdown" => 0,  
  8. "hide_empty" => 0,  
  9. "orderby" => "order" 
  10. ), $atts ); 
  11.  
  12. $c = $atts['show_count']; 
  13. $h = $atts['hierarchical'];  
  14. $s = $atts['show_children_only']; 
  15. $d = $atts['dropdown']; 
  16. $e = $atts['hide_empty']; 
  17. $o = $atts['orderby']; 
  18. $dropdown_args = array( 'hide_empty' => $e ); 
  19. $list_args = array( 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat', 'hide_empty' => $e ); 
  20.  
  21. // Menu Order 
  22. $list_args['menu_order'] = false; 
  23. if ( $o == 'order' ) { 
  24. $list_args['menu_order'] = 'asc'; 
  25. } else { 
  26. $list_args['orderby'] = 'title'; 
  27.  
  28. // Setup Current Category 
  29. $current_cat = false; 
  30. $cat_ancestors = array(); 
  31. if ( is_tax( 'product_cat' ) ) { 
  32. $current_cat = $wp_query->queried_object; 
  33. $cat_ancestors = get_ancestors( $current_cat->term_id, 'product_cat' ); 
  34. } elseif ( is_singular( 'product' ) ) { 
  35. $product_category = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent' ) ); 
  36. if ( $product_category ) { 
  37. $current_cat = end( $product_category ); 
  38. $cat_ancestors = get_ancestors( $current_cat->term_id, 'product_cat' ); 
  39.  
  40. // Show Siblings and Children Only 
  41. if ( $s && $current_cat ) { 
  42. // Top level is needed 
  43. $top_level = get_terms( 
  44. 'product_cat',  
  45. array( 
  46. 'fields' => 'ids',  
  47. 'parent' => 0,  
  48. 'hierarchical' => true,  
  49. 'hide_empty' => false 
  50. ); 
  51.  
  52. // Direct children are wanted 
  53. $direct_children = get_terms( 
  54. 'product_cat',  
  55. array( 
  56. 'fields' => 'ids',  
  57. 'parent' => $current_cat->term_id,  
  58. 'hierarchical' => true,  
  59. 'hide_empty' => false 
  60. ); 
  61.  
  62. // Gather siblings of ancestors 
  63. $siblings = array(); 
  64. if ( $cat_ancestors ) { 
  65. foreach ( $cat_ancestors as $ancestor ) { 
  66. $ancestor_siblings = get_terms( 
  67. 'product_cat',  
  68. array( 
  69. 'fields' => 'ids',  
  70. 'parent' => $ancestor,  
  71. 'hierarchical' => false,  
  72. 'hide_empty' => false 
  73. ); 
  74. $siblings = array_merge( $siblings, $ancestor_siblings ); 
  75. if ( $h ) { 
  76. $include = array_merge( $top_level, $cat_ancestors, $siblings, $direct_children, array( $current_cat->term_id ) ); 
  77. } else { 
  78. $include = array_merge( $direct_children ); 
  79. $dropdown_args['include'] = implode( ', ', $include ); 
  80. $list_args['include'] = implode( ', ', $include ); 
  81.  
  82. if ( empty( $include ) ) { 
  83. return; 
  84. } elseif ( $s ) { 
  85. $dropdown_args['depth'] = 1; 
  86. $dropdown_args['child_of'] = 0; 
  87. $dropdown_args['hierarchical'] = 1; 
  88. $list_args['depth'] = 1; 
  89. $list_args['child_of'] = 0; 
  90. $list_args['hierarchical'] = 1; 
  91.  
  92. // Dropdown 
  93. if ( $d ) { 
  94. $dropdown_defaults = array( 
  95. 'show_counts' => $c,  
  96. 'hierarchical' => $h,  
  97. 'show_uncategorized' => 0,  
  98. 'orderby' => $o,  
  99. 'selected' => $current_cat ? $current_cat->slug : '' 
  100. ); 
  101. $dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults ); 
  102. //wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) ); 
  103.  
  104. $current_product_cat = isset( $wp_query->query['product_cat'] ) ? $wp_query->query['product_cat'] : ''; 
  105.  
  106. $terms = get_terms( 'product_cat', apply_filters( 'wc_product_dropdown_categories_get_terms_args', $dropdown_args ) ); 
  107.  
  108. if ( ! $terms ) { 
  109. return; 
  110.  
  111. $content = $content != '' ? $content : __( 'Select a category' ); 
  112. $cat_drop = "<select name='product_cat' class='dropdown_product_cat'>"; 
  113. $cat_drop .= '<option value="" ' . selected( $current_product_cat, '', false ) . '>' . $content . '</option>'; 
  114. $cat_drop .= wc_walk_category_dropdown_tree( $terms, 0, $dropdown_args ); 
  115. $cat_drop .= "</select>";  
  116.  
  117. wc_enqueue_js( " 
  118. jQuery('.dropdown_product_cat').change(function() { 
  119. if(jQuery(this).val() != '') { 
  120. location.href = '" . home_url() . "/?product_cat=' + jQuery(this).val(); 
  121. }); 
  122. " ); 
  123.  
  124. return $cat_drop; 
  125.  
  126. // List 
  127. } else { 
  128. $list_args['echo'] = 0; 
  129. $list_args['title_li'] = ''; 
  130. $list_args['pad_counts'] = 1; 
  131. $list_args['show_option_none'] = __('No product categories exist.', 'woocommerce' ); 
  132. $list_args['current_category'] = ( $current_cat ) ? $current_cat->term_id : ''; 
  133. $list_args['current_category_ancestors'] = $cat_ancestors; 
  134.  
  135. $content = $content != '' ? $content : __( 'Product Categories' ); 
  136. $cat_list = '<h2 class="categories-title">'.$content.'</h2>'; 
  137. $cat_list .= '<ul class="product-categories">'; 
  138. $cat_list .= wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $list_args ) ); 
  139. $cat_list .= '</ul>'; 
  140.  
  141. return $cat_list;