Filter orders with attributes and/or terms
-
Hello guys. Hope you do well. I was trying to implement order filter functionality but nothing comes up. If there is any plugin to filter orders by terms please tell me even if prem. Also I tryed some code. The filtering dropdown come well but any filtering shows no order found.
Here is the code`/** * Add attribute filter dropdown to WooCommerce order page */ add_action('restrict_manage_posts', 'woocommerce_attribute_filter'); function woocommerce_attribute_filter() { global $typenow, $wpdb; // Check if we're on the orders page if ($typenow === 'shop_order') { $attributes = wc_get_attribute_taxonomies(); if (!empty($attributes)) { foreach ($attributes as $attribute) { $attribute_name = $attribute->attribute_name; $attribute_label = $attribute->attribute_label; $taxonomy = wc_attribute_taxonomy_name($attribute_name); $terms = get_terms($taxonomy, array('hide_empty' => false)); if (!empty($terms)) { $selected_term = isset($_GET['attribute_filter_' . $attribute_name]) ? $_GET['attribute_filter_' . $attribute_name] : ''; echo '<select name="attribute_filter_' . esc_attr($attribute_name) . '">'; echo '<option value="">All ' . esc_html($attribute_label) . '</option>'; foreach ($terms as $term) { $selected = $selected_term === $term->slug ? 'selected' : ''; echo '<option value="' . esc_attr($term->slug) . '" ' . $selected . '>' . esc_html($term->name) . '</option>'; } echo '</select>'; } } } } } /** * Filter orders based on the selected attribute */ add_action('pre_get_posts', 'woocommerce_attribute_filter_query'); function woocommerce_attribute_filter_query($query) { global $pagenow, $typenow, $wpdb; // Check if we're on the orders page if ($pagenow === 'edit.php' && $typenow === 'shop_order' && isset($_GET['post_type']) && $_GET['post_type'] === 'shop_order') { // Check if any attribute filters are set $attribute_filters = array(); foreach ($_GET as $query_var => $query_value) { if (strpos($query_var, 'attribute_filter_') === 0) { $attribute_name = substr($query_var, 17); $attribute_filters[$attribute_name] = $query_value; } } if (!empty($attribute_filters)) { $meta_query = array('relation' => 'OR'); foreach ($attribute_filters as $attribute_name => $term) { $taxonomy = wc_attribute_taxonomy_name($attribute_name); $meta_query[] = array( 'key' => '_product_attributes', 'value' => ':"' . $taxonomy . '";', 'compare' => 'LIKE', ); } $query->set('meta_query', $meta_query); $query->set('post_type', 'shop_order'); } } }
The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Filter orders with attributes and/or terms’ is closed to new replies.