Include hidden 'product category' to search 'woocommerce' - wordpress

**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 :)

Related

Exclude custom taxonomy from product query

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 );
}

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 .

How to hide products from the products shortcode 'woocommerce_shortcode_products_query' using product ids

I am using the woocommerce products shortcode to show some related products on a product page.
The product shortcode is as follows:
do_shortcode('[products limit="6" columns="6"]');
I am modifying the shortcode using the woocommerce_shortcode_products_query filter like so:
add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop ){
$product_id = 12345;
// Remove out of stock results
$query_args['meta_query'] = array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) );
// Remove the current product by id
$query_args['tax_query'] = array( array(
'taxonomy' => 'product',
'field' => 'id',
'terms' => array($product_id), // Remove this product from the shortcode results
'operator' => 'NOT IN',
) );
return $query_args;
}, 10, 3);
The first part works by removing the out of stock results.
The second part does not work and I am unable to work out how to exclude a specific product from the query.
All results from google when searching the problem are talking about product_cat taxonomies, however I do not want to remove products in a category, I want to remove the current product by id so that only other related products show in the shortcode.
So my question: How do you hide the current product from the products shortcode using 'woocommerce_shortcode_products_query'
You would need to use the 'post__not_in' argument of the WP_Query.
So your code would be something like this:
add_filter('woocommerce_shortcode_products_query', function ($query_args, $atts, $loop) {
$product_ids = array(1, 2, 3);
$query_args['meta_query'] = array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
));
$query_args['post__not_in'] = $product_ids;
return $query_args;
}, 10, 3);
This will exclude products with the ids of 1, 2 and 3. You could pass different product ids to the $product_ids variable.
You could exclude only one product as well, by just passing one single id to the $product_ids variable! (e.g. $product_ids = array(37))

Woocommerce - Only show grouped products parent in the loop

I'm building a store for books. I have a bunch of grouped products. My pain is, that woocommerce lists both type by default. I only need the parent of the group to be listed without childrens.
Is there any hook or workaround for this?
Thanks.
Just to add to this, there is a way to do this without having to code anything.
You can simply choose to hide the product using the visibility option.
The quickest way to do this is to go to Products > Products and tick all the products you want to hide. Click the Bulk Actions drop-down and choose Hidden in the Visibility drop-down.
Some CSV import plugins should have this option too. I use Woocommerce Import CSV plugin and set the heading 'visibility' to hidden for the products I don't want to show in the catalog or search.
I have to hide all but the parent products for a price comparison site.
Hope this helps anyone looking for similar solution.
The above answers do not work when you perform filters
This works with filters and does not overwrite other queries:
add_action( 'woocommerce_product_query', 'only_grouped_products_query' );
function only_grouped_products_query( $q ) {
//get current loop query
$taxonomy_query = $q->get('tax_query') ;
//appends the grouped products condition
$taxonomy_query['relation'] = 'AND';
$taxonomy_query[] = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'grouped'
);
$q->set( 'tax_query', $taxonomy_query );
}
Try the following:
add_action( 'woocommerce_product_query', 'so_27975262_product_query' );
function so_27975262_product_query( $q ){
$q->set( 'post_parent', 0 );
}
The idea is that we're modifying the query such that it will only show top-level items.... thus (in theory) nothing that has been assigned to a group, which would then have the group product's ID as the post_parent.
Try the following code:
add_action( 'woocommerce_product_query', 'hs_product_query' );
function hs_product_query( $q ){
$product_type = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'grouped'
);
$q->set('tax_query', array($product_type) );
$q->set( 'post_parent', 0 );
}
It worked for me..
Ok late answer but also found this solution:
$params = array(
'posts_per_page' => 5,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => 'grouped',
),
),
);
Customized Product Query using WP_Query - WooCommerce

Remove woocommerce hidden products from wordpress default search result

I need to remove woocommerce hidden products from default wordpress search result, hidden products are only hidden when doing a woocommerce search. Tnx in advance.
Alter the search loop and use the following codes below
$_pf = new WC_Product_Factory();
$_product = $_pf->get_product($id); // assuming $id is available as the code is inside the loop
$_product->is_visible()
In Woocommerce 3+ this changed from metadata to a taxonomy
add_action( 'pre_get_posts', 'search_query_fix' );
function search_query_fix( $query = false ) {
if(!is_admin() && is_search()){
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'exclude-from-search',
'operator' => 'NOT IN',
)
));
}
}

Resources