Reputation: 23
I'm currently working on woocommerce, to make an specific request before displaying products. I'm using for that the hook woocommerce_product_query
that is executed before the request is done in the class WC_Query in product_query
method.
I can access the variable $q (query object), example bellow:
Now I would like to know how to filter this request by some custom attributes. I thought that would be something like that, but nothing is working maybe you could explain why.
add_action( 'woocommerce_product_query', 'filter_products_by_brand' );
function filter_products_by_brand( WP_Query $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'cb-brand',
'field' => 'slug',
'terms' => array( 'jeep' ),
'operator' => 'IN'
);
$q->set( 'tax_query', $tax_query );
}
WP_Query Object
(
[query] => Array
(
[product_cat] => accessoires-2/attelages/faisceaux
)
[query_vars] => Array
(
[product_cat] => faisceaux
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[orderby] => menu_order title
[order] => ASC
[meta_query] => Array
(
)
[tax_query] => Array
(
[relation] => AND
[0] => Array
(
[taxonomy] => product_visibility
[field] => term_taxonomy_id
[terms] => Array
(
[0] => 7
)
[operator] => NOT IN
)
)
[wc_query] => product_query
[posts_per_page] => 16
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => product_cat
[terms] => Array
(
[0] => faisceaux
)
[field] => slug
[operator] => IN
[include_children] => 1
)
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
[product_cat] => Array
(
[terms] => Array
(
[0] => faisceaux
)
[field] => slug
)
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[queried_object] => WP_Term Object
(
[term_id] => 108
[name] => Faisceaux
[slug] => faisceaux
[term_group] => 0
[term_taxonomy_id] => 108
[taxonomy] => product_cat
[description] =>
[parent] => 106
[count] => 1
[filter] => raw
)
[queried_object_id] => 108
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] => 1
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_privacy_policy] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_favicon] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => a80e55b982d04f2e0de36fdd19a948d6
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
Thanks.
Upvotes: 0
Views: 1355
Reputation: 23
If anyone wants to know.
To make an filter based on an attributes :
add_filter('woocommerce_product_query_tax_query', 'custom_product_query_meta_query', 10, 2);
function custom_product_query_meta_query( $tax_query, $query ) {
$taxonomy = 'pa_${NAME OF THE ATTRIBUTE}'; // Note: always start with "pa_" in Woocommerce
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'name', // name or slug
'terms' => array( '1S026015002003' ),
'operator' => 'IN',
);
return $tax_query;
}
Upvotes: 1