Reputation: 51
In Woocommerce, on the admin products list, I should need to filter products for a specific attribute.
Based on "Add a filter dropdown for product tags in woocommerce admin product list" answer code, I have made some changes to filter products for pa_proizvoditel
product attribute:
add_action('restrict_manage_posts', 'product_attr_sorting');
function product_attr_sorting() {
global $typenow;
$taxonomy = 'pa_proizvoditel';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Attr {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_filter('parse_query', 'product_attr_sorting_query');
function product_attr_sorting_query($query) {
global $pagenow;
$taxonomy = 'pa_proizvoditel';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
But when you select a value and click on the "filter" button, the page is updated, but the products are not filtered (a complete list of all products).
Any help to solve this problem issue is appreciated.
Спасибо за помощь. Понял код работает (первый код пробовал в версии 2.5) на версиях woocommerce ниже 3 Вот код, который работает на версии выше 3 (проверено на 3.5+) Я не знаю, в чем дело, но у меня есть
function filter_proizvoditel_admin_sort( &$query )
{
if (
is_admin()
AND 'edit.php' === $GLOBALS['pagenow']
AND isset( $_GET['pa_proizvoditel'] )
AND '-1' != $_GET['pa_proizvoditel']
)
{
$query->query_vars['tax_query'] = array( array(
'taxonomy' => 'pa_proizvoditel'
,'field' => 'ID'
,'terms' => array( $_GET['pa_proizvoditel'] )
) );
}
}
add_filter( 'parse_query', 'filter_proizvoditel_admin_sort' );
function filter_proizvoditel_form()
{
$selected = isset($_GET['pa_proizvoditel']) ? $_GET['pa_proizvoditel'] : '';
wp_dropdown_categories( array(
'taxonomy' => 'pa_proizvoditel',
'hide_empty' => 0,
'name' => 'pa_proizvoditel',
'show_option_none' => 'Все производители',
'show_count' => false,
'hide_empty' => true,
'selected' => $selected
) );
}
add_action( 'restrict_manage_posts', 'filter_proizvoditel_form', 25 );
Upvotes: 5
Views: 3895
Reputation: 51
I changed your code a little bit and now this works 100%.
My product attribute slug is "dobavitelj_xml" but you have to append "pa_" so taxonomy slug in my case is "pa_dobavitelj_xml".
add_action('restrict_manage_posts', 'product_attribute_sorting_dropdown');
function product_attribute_sorting_dropdown() {
global $typenow;
$taxonomy = 'pa_dobavitelj_xml';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("{$info_taxonomy->labels->name}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => false,
));
};
}
add_action('parse_query', 'product_attribute_sorting_query');
function product_attribute_sorting_query( $query ) {
global $pagenow;
$taxonomy = 'pa_dobavitelj_xml';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($_GET[$taxonomy]) && is_numeric($_GET[$taxonomy]) && $_GET[$taxonomy] != 0) {
$tax_query = (array) $query->get('tax_query');
$term = get_term_by('id', $_GET[$taxonomy], $taxonomy);
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => array($term->slug),
'operator' => 'AND'
);
$query->set( 'tax_query', $tax_query );
}
}
Upvotes: 5
Reputation: 253784
I have tested your version code and it works for any defined product attribute taxonomy. Now there is some minors mistakes like parse_query
hook that is an action hook (but not a filter hook).
For example I use the product attribute taxonomy pa_color
in the revisited code below:
add_action('restrict_manage_posts', 'product_attribute_sorting_dropdown');
function product_attribute_sorting_dropdown() {
global $typenow;
$taxonomy = 'pa_color';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Attribute {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_action('parse_query', 'product_attribute_sorting_query');
function product_attribute_sorting_query( $query ) {
global $pagenow;
$taxonomy = 'pa_color';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works on Woocommerce version 3+ (for sure).
I get the following Product Attribute dropdown:
Then when I filter with the blue (5)
term, I get the correct number of filtered products:
Related: Add a filter dropdown for product tags in woocommerce admin product list
Upvotes: 4