/**
 * WooCommerce Dropdown categories.
 *
 * Stuck with this until a fix for https://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_count'] = $args;
        $args['hierarchical'] = $deprecated_hierarchical;
        $args['show_uncategorized'] = $deprecated_show_uncategorized;
        $args['orderby'] = $deprecated_orderby;
    }
    $current_product_cat = isset($wp_query->query_vars['product_cat']) ? $wp_query->query_vars['product_cat'] : '';
    $defaults = array('pad_counts' => 1, 'show_count' => 1, 'hierarchical' => 1, 'hide_empty' => 1, 'show_uncategorized' => 1, 'orderby' => 'name', 'selected' => $current_product_cat, 'menu_order' => false);
    $args = wp_parse_args($args, $defaults);
    if ('order' === $args['orderby']) {
        $args['menu_order'] = 'asc';
        $args['orderby'] = 'name';
    }
    $terms = get_terms('product_cat', apply_filters('wc_product_dropdown_categories_get_terms_args', $args));
    if (empty($terms)) {
        return;
    }
    $output = "<select name='product_cat' class='dropdown_product_cat'>";
    $output .= '<option value="" ' . selected($current_product_cat, '', false) . '>' . __('Select a category', 'woocommerce') . '</option>';
    $output .= wc_walk_category_dropdown_tree($terms, 0, $args);
    if ($args['show_uncategorized']) {
        $output .= '<option value="0" ' . selected($current_product_cat, '0', false) . '>' . __('Uncategorized', 'woocommerce') . '</option>';
    }
    $output .= "</select>";
    echo $output;
}
    /**
     * Category selection
     * @return void
     */
    public function category_widget()
    {
        $categories = get_terms('product_cat', array('orderby' => 'name'));
        ?>
		<form method="GET">
			<div>
				<select multiple="multiple" data-placeholder="<?php 
        _e('Select categories&hellip;', 'woocommerce');
        ?>
" class="chosen_select" id="show_categories" name="show_categories[]" style="width: 205px;">
					<?php 
        $r = array();
        $r['pad_counts'] = 1;
        $r['hierarchical'] = 1;
        $r['hide_empty'] = 1;
        $r['value'] = 'id';
        $r['selected'] = $this->show_categories;
        include_once WC()->plugin_path() . '/includes/walkers/class-product-cat-dropdown-walker.php';
        echo wc_walk_category_dropdown_tree($categories, 0, $r);
        ?>
				</select>
				<a href="#" class="select_none"><?php 
        _e('None', 'woocommerce');
        ?>
</a>
				<a href="#" class="select_all"><?php 
        _e('All', 'woocommerce');
        ?>
</a>
				<input type="submit" class="submit button" value="<?php 
        _e('Show', 'woocommerce');
        ?>
" />
				<input type="hidden" name="range" value="<?php 
        if (!empty($_GET['range'])) {
            echo esc_attr($_GET['range']);
        }
        ?>
" />
				<input type="hidden" name="start_date" value="<?php 
        if (!empty($_GET['start_date'])) {
            echo esc_attr($_GET['start_date']);
        }
        ?>
" />
				<input type="hidden" name="end_date" value="<?php 
        if (!empty($_GET['end_date'])) {
            echo esc_attr($_GET['end_date']);
        }
        ?>
" />
				<input type="hidden" name="page" value="<?php 
        if (!empty($_GET['page'])) {
            echo esc_attr($_GET['page']);
        }
        ?>
" />
				<input type="hidden" name="tab" value="<?php 
        if (!empty($_GET['tab'])) {
            echo esc_attr($_GET['tab']);
        }
        ?>
" />
				<input type="hidden" name="report" value="<?php 
        if (!empty($_GET['report'])) {
            echo esc_attr($_GET['report']);
        }
        ?>
" />
			</div>
			<script type="text/javascript">
				jQuery(function(){
					jQuery("select.chosen_select").chosen();

					// Select all/none
					jQuery( '.chart-widget' ).on( 'click', '.select_all', function() {
						jQuery(this).closest( 'div' ).find( 'select option' ).attr( "selected", "selected" );
						jQuery(this).closest( 'div' ).find('select').trigger( 'chosen:updated' );
						return false;
					});

					jQuery( '.chart-widget').on( 'click', '.select_none', function() {
						jQuery(this).closest( 'div' ).find( 'select option' ).removeAttr( "selected" );
						jQuery(this).closest( 'div' ).find('select').trigger( 'chosen:updated' );
						return false;
					});
				});
			</script>
		</form>
		<?php 
    }
function woocommerce_walk_category_dropdown_tree()
{
    return wc_walk_category_dropdown_tree();
}
/**
 * @deprecated
 */
function woocommerce_walk_category_dropdown_tree($a1 = '', $a2 = '', $a3 = '')
{
    return wc_walk_category_dropdown_tree($a1, $a2, $a3);
}
Example #5
0
function aiwoo_shortcode_productcat($atts, $content = null)
{
    global $woocommerce, $wp_query, $post;
    $atts = shortcode_atts(array("show_count" => 0, "hierarchical" => 0, "show_children_only" => 0, "dropdown" => 0, "hide_empty" => 0, "orderby" => "order"), $atts);
    $c = $atts['show_count'];
    $h = $atts['hierarchical'];
    $s = $atts['show_children_only'];
    $d = $atts['dropdown'];
    $e = $atts['hide_empty'];
    $o = $atts['orderby'];
    $dropdown_args = array('hide_empty' => $e);
    $list_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat', 'hide_empty' => $e);
    // Menu Order
    $list_args['menu_order'] = false;
    if ($o == 'order') {
        $list_args['menu_order'] = 'asc';
    } else {
        $list_args['orderby'] = 'title';
    }
    // Setup Current Category
    $current_cat = false;
    $cat_ancestors = array();
    if (is_tax('product_cat')) {
        $current_cat = $wp_query->queried_object;
        $cat_ancestors = get_ancestors($current_cat->term_id, 'product_cat');
    } elseif (is_singular('product')) {
        $product_category = wc_get_product_terms($post->ID, 'product_cat', array('orderby' => 'parent'));
        if ($product_category) {
            $current_cat = end($product_category);
            $cat_ancestors = get_ancestors($current_cat->term_id, 'product_cat');
        }
    }
    // Show Siblings and Children Only
    if ($s && $current_cat) {
        // Top level is needed
        $top_level = get_terms('product_cat', array('fields' => 'ids', 'parent' => 0, 'hierarchical' => true, 'hide_empty' => false));
        // Direct children are wanted
        $direct_children = get_terms('product_cat', array('fields' => 'ids', 'parent' => $current_cat->term_id, 'hierarchical' => true, 'hide_empty' => false));
        // Gather siblings of ancestors
        $siblings = array();
        if ($cat_ancestors) {
            foreach ($cat_ancestors as $ancestor) {
                $ancestor_siblings = get_terms('product_cat', array('fields' => 'ids', 'parent' => $ancestor, 'hierarchical' => false, 'hide_empty' => false));
                $siblings = array_merge($siblings, $ancestor_siblings);
            }
        }
        if ($h) {
            $include = array_merge($top_level, $cat_ancestors, $siblings, $direct_children, array($current_cat->term_id));
        } else {
            $include = array_merge($direct_children);
        }
        $dropdown_args['include'] = implode(',', $include);
        $list_args['include'] = implode(',', $include);
        if (empty($include)) {
            return;
        }
    } elseif ($s) {
        $dropdown_args['depth'] = 1;
        $dropdown_args['child_of'] = 0;
        $dropdown_args['hierarchical'] = 1;
        $list_args['depth'] = 1;
        $list_args['child_of'] = 0;
        $list_args['hierarchical'] = 1;
    }
    // Dropdown
    if ($d) {
        $dropdown_defaults = array('show_counts' => $c, 'hierarchical' => $h, 'show_uncategorized' => 0, 'orderby' => $o, 'selected' => $current_cat ? $current_cat->slug : '');
        $dropdown_args = wp_parse_args($dropdown_args, $dropdown_defaults);
        //wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) );
        $current_product_cat = isset($wp_query->query['product_cat']) ? $wp_query->query['product_cat'] : '';
        $terms = get_terms('product_cat', apply_filters('wc_product_dropdown_categories_get_terms_args', $dropdown_args));
        if (!$terms) {
            return;
        }
        $content = $content != '' ? $content : __('Select a category');
        $cat_drop = "<select name='product_cat' class='dropdown_product_cat'>";
        $cat_drop .= '<option value="" ' . selected($current_product_cat, '', false) . '>' . $content . '</option>';
        $cat_drop .= wc_walk_category_dropdown_tree($terms, 0, $dropdown_args);
        $cat_drop .= "</select>";
        wc_enqueue_js("\n\t\t\tjQuery('.dropdown_product_cat').change(function(){\n\t\t\t\tif(jQuery(this).val() != '') {\n\t\t\t\t\tlocation.href = '" . home_url() . "/?product_cat=' + jQuery(this).val();\n\t\t\t\t}\n\t\t\t});\n\t\t");
        return $cat_drop;
        // List
    } else {
        $list_args['echo'] = 0;
        $list_args['title_li'] = '';
        $list_args['pad_counts'] = 1;
        $list_args['show_option_none'] = __('No product categories exist.', 'woocommerce');
        $list_args['current_category'] = $current_cat ? $current_cat->term_id : '';
        $list_args['current_category_ancestors'] = $cat_ancestors;
        $content = $content != '' ? $content : __('Product Categories');
        $cat_list = '<h2 class="categories-title">' . $content . '</h2>';
        $cat_list .= '<ul class="product-categories">';
        $cat_list .= wp_list_categories(apply_filters('woocommerce_product_categories_widget_args', $list_args));
        $cat_list .= '</ul>';
        return $cat_list;
    }
}