Exclude products with product shortocde Woocommerce - wordpress

I am trying to exclude products with shortcode but its not working
here is what i am using shortocode
[products limit="14" orderby="id" columns="4" order="DESC" visibility="visible" category="tcs" cat_operator="NOT IN"]
I used php filter also
function phl_customize_product_shortcode( $args, $atts ) {
if ( is_front_page() ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array( 'tcs' ),
'operator' => 'NOT IN'
)
);
}
return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'phl_customize_product_shortcode', 10, 2 );
But still not working
here is site you can see
https://www.pottershouselimited.co.uk

can you try this:
function phl_customize_product_shortcode( $args, $atts ) {
if ( is_front_page() ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'tcs' ),
'operator' => 'NOT IN'
)
);
}
return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'phl_customize_product_shortcode', 10, 2 );
and tell me how it goes? also where on the website can i see the tcs items on homepage?

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

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?

Custom shortcode for parent and child taxonomy not working

I got a shortcode to print out the brand name connected to the current page:
add_shortcode( 'MARKE', 'marke_shortcode' );
function marke_shortcode() {
$terms = array(
'post_type' => 'fahrzeuge',
'taxonomy' => 'marken',
'hide_empty' => false,
'fields' => 'ids'
);
if( is_tax('marken') ) {
$terms['tax_query'] = array(
array(
'taxonomy' => 'marken',
'field' => 'term_id',
'terms' => (array) get_queried_object_id(),
)
);
}
$posts = get_posts( $terms );
if( ! empty( $posts ) )
return get_field( 'marken', $posts[0] );
return '0';
}
Unfortunately the code isn't working.
What I try to achieve is that the brand name, e.g. Tesla is printed out with the shortcode [MARKE] on both type of pages:
https://moinmobility.de/marken/tesla/
https://moinmobility.de/marken/tesla/model-3/
I tried a lot of code chunks and always if the brand name was printed correctly on the parent page, the child one was wrong.

How to Show Featured products by current product category

By default the widget shows all featured products from all categories, but it would be more logical to show only featured products from the category whose product or category the user is currently browsing.
I try this, but not work
add_filter( 'woocommerce_products_widget_query_args', function ( $query_args ){
$categories = wp_get_post_categories( get_queried_object_id() );
$query_args['tax_query'] = array( array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'field' => 'slug',
'terms' => $categories,
'posts_per_page' => -1,
));
return $query_args;
}, 10, 1 );
And this not work
/**
* Featured Products only from currently category
*/
add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
$term = get_queried_object();
$category_id = empty( $term->term_id ) ? 0 : $term->term_id;
$query_args['tax_query'] = array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat,
));
return $query_args;
}, 10, 1 );
And this not work
add_filter( 'woocommerce_products_widget_query_args', 'r2gqo', 1 );
function r2gqo( $query_args ) {
$category = get_queried_object();
$current_cat= $category->term_id;
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $term_id,
);
return $query_args;
}
This not work
add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
$category = get_queried_object();
$current_cat= $category->term_id;
$query_args['tax_query'] = array( array(
'terms' => $category,
));
return $query_args;
}, 10, 1 );
And it not work
add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
$term = get_queried_object();
$category_id = empty( $term->term_id ) ? 0 : $term->term_id;
$query_args['tax_query'] = array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category_id,
));
return $query_args;
}, 10, 1 );
This work only for certains category
add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
$categories = array( 'music', 'posters' );
$query_args['tax_query'] = array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $categories,
));
return $query_args;
}, 10, 1 );
Just a draft bellow
/* //draft
used before
global $wp_query;
if(is_product_category()) {
$current_term = get_queried_object(); }
$taxonomies = get_object_taxonomies( get_post_type( $post_id ) );
return $wp_query->get_queried_object();
*/
Combining your one that works for specific categories only, and the draft that you had started. I have tried the following and it works for category pages and single product pages. So on category pages it will filter by the category of the page and on single product pages it will filter by any of the categories the product is assigned to
add_filter( 'woocommerce_products_widget_query_args', function( $query_args ){
$cat = '';
if ( is_product_category() ) { // is it a product category page
$current_term = get_queried_object();
// get_queries_object returns a WP_Term object when on a category page
$cat = $current_term->term_id;
} elseif ( is_product() ) { // or is it a single product page
global $product;
// get_category_ids will return an array of all categories assigned to the product
$cat = $product->get_category_ids();
}
$query_args['tax_query'] = array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat,
));
return $query_args;
}, 10, 1 );
Further to requirements, if you only want featured products, you would then use
$query_args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat,
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
));
If you are using multiple product list widgets the above will prevent them all working correctly. So you would instead use
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cat,
);
which adds to the existing tax_query instead of replacing it. Then you can set Show "Featured products" in the actual product list widget settings

Woocommerce product search in admin is not working

I have a list of products in woocommerce
when I search for a product (in admin), it always says "No Products found":
This is the URL /wp-admin/edit.php?s=Deluxe&post_status=all&post_type=product&action=-1&m=0&product_cat&product_type&paged=1&action2=-1
and when I remove product_cat from the URL the product comes up
Add in the following snippet to function.php:
add_action( 'pre_get_posts', 'products_pre_get_posts' );
function products_pre_get_posts( $query ) {
if(is_admin()){
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
)
));
}
}
I had the same problem. After disable the YoastSeo plugin, the search works properly again.
The Answer from Chandrakant Devani helped, but broke other searches in the admin. Adding an if seems to avoid breakages
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product')
full code:
add_action( 'pre_get_posts', 'products_pre_get_posts' );
function products_pre_get_posts( WP_Query $query ) {
if ( is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'product' ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_terms( array( 'taxonomy' => 'product_cat', 'fields' => 'ids' ) )
)
) );
}
}

Resources