Exclude custom taxonomy from product query - wordpress

In a woocommerce installation I added custom taxonomies for products. One of them is season with a term 'sale'.
I want to exclude these sale items from the shop page. So I used below function, but it doesn't seem to work. Is this function actually working for a custom taxonomy?
Source code: https://woocommerce.com/document/exclude-a-category-from-the-shop-page/
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
global $woocommerce;
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'season', // taxonomy
'field' => 'slug',
'terms' => array( 'sale' ), //slug taxonomies
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}

Related

Woocommerce woocommerce_product_query hook is not working

I need to hide a few products from the shop page. And I'm doing it by using a taxonomy called subscription_tier but following code doesn't work.
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'subscription_tiers',
'field' => 'slug',
'terms' => array( 'standard' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
I even tried changing 'taxonomy' => 'product_cat', and 'terms' => array( 'women' ). But this wont affect my results

Include hidden 'product category' to search 'woocommerce'

**Trying to include a hidden single category to search.
**
I've a category which I don't want to enlisted on Shop Page. I have created a separate menu item for it, which is where I want to list all the product from the category. The code i used work fine but when i go to this category and i try to search for a product i receive this:
**No products were found matching your selection.
**
I already used this code:
`
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if (!$q->is_main_query() || !is_shop()) return;
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'library' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
`
So i want to include this category in the search option and in the same time hide it from the shop page.Thanks :)

Exclude products from a certain category, with Woocommerce and Elementor Anywhere

I am using Elementor Anywhere to create a specific display for my WooCommerce products.
I have a filter and for the filter works I need to use Post blocks Adv.
There I ask to display current archive.
I have the possibility to put a query filter, but I can't exclude products from a certain category.
I have this, but it doesn't work with current archives.
function filtrer_produit_sans_crea( $query_args ) {
$query_args['meta_query'] = array(
array(
'key' => '_stock_status',
'value' => 'instock',
),
);
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'coin-des-creatrices',
'operator' => 'NOT IN',
);
return $query_args;
}
add_filter( 'filtre_zero_dechet', 'filtrer_produit_sans_crea' );
Has anyone ever tried something like this?
Or do you have a clue?
OK I found a solution:
function mystore_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );
But it does on the whole site.
If anyone has a clue how to do it only on a specific page...
Naively I wanted to add this:
function mystore_pre_get_posts_query( $q ) {
if ( is_page( 54 ) ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
add_action( 'woocommerce_product_query', 'mystore_pre_get_posts_query' );
But it's doesn't work ^_^
I found another track.
function mystore_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'coin-des-creatrices' ), // modifier la catégorie
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'wp_head', 'remove_my_action');
function remove_my_action($post) {
global $post;
$post_id = $post->ID;
if (160029 == $post_id) {
echo "PAGE OK";
add_action( 'pre_get_posts', 'mystore_pre_get_posts_query');
}else{
remove_action('pre_get_posts', 'mystore_pre_get_posts_query');
}
}
My "page ok" displays well but the function does not launch.
If it is outside this function, the line add_action('pre_get_posts', 'mystore_pre_get_posts_query');
does what I want it to do.
Is it add_action( 'wp_head', 'remove_my_action'); that is not the right one?

Display Woocommerce Related Products by current Category and tags

Please don't judge this question harshly.
I have tried showing in Related Products only products from the same category in which the user is viewing the product right now, and by product tags (if applicable). But nothing has changed for me.
It turns out that WooCommerce already displays recommended products from the same category and by the same tags by default.
Below you will find the answer to this question. But let this code remain here simply as a sample code.
function related_products_by_current_category( $related_posts, $product, $args ) {
global $post;
$cat = $product->get_category_ids();
$tags = wp_get_post_terms( $post->ID, "product_tag" );
foreach ( $tags as $tag ) {
$tags_array[] .= $tag->term_id;
}
$related_posts = new WP_Query(
array(
'orderby' => 'rand',
'posts_per_page' => 4,
'post__not_in' => array($post->ID),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat
)
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
)
);
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'related_products_by_current_category', 999, 3 );
WooCommerce already fetches related products based on the same terms( categories and tags )
If you want to get more control over the related products by categories and tags, you can use these two filters
apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id );
apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id );
These filters return the current product categories | tags IDs which will be used to get related products. you can use these filters to return specific terms IDs based on the given product ID .

woo commerce shop page apply custom filter

I want to display particular authors' products on the shop page.
but I don't know how can I do it I tried to do using the below code.
/**
* Exclude products from a particular category on the shop page
*/
function custom_pre_get_posts_query($q)
{
$user_id = 44;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'fields' => 'ids',
'author' => $user_id
);
$loop = new WP_Query($args);
echo '<pre>';
print_r($loop->posts);
echo '</pre>';
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('camisetas'), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set('tax_query', $tax_query);
}
add_action('woocommerce_product_query', 'custom_pre_get_posts_query');
now I get all products of a particular user id in my case it 44.
but I don't know how to implement it in woo-commerce tax query.
You can pass author to query.
function custom_pre_get_posts_query( $q ){
if( is_admin() ) return;
$user_id = 44;
$q->set( 'author', $user_id );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query', 10, 1 );

Resources