How disable unused terms in filter?
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page pleaseIf you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.
Quote from Andrii on August 11, 2023, 09:23I need to hide products on my shop which are included in grouped products. I wrote function which does it:
add_filter('woof_products_query', 'woof_hide_grouped_products_from_category', 100); function woof_hide_grouped_products_from_category($query_vars) { $taxonomy = $query_vars['tax_query'][0]['taxonomy']; $term_slug = $query_vars['tax_query'][0]['terms'][0]; $term = get_term_by('slug', $term_slug, $taxonomy); $term_id = $term ? $term->term_id : 0; if (isset($taxonomy) && $taxonomy && isset($term_id) && $term_id) { $child_product_ids = get_grouped_children_ids($term_id, $taxonomy); if (!empty($child_product_ids)) { $query_vars['post__not_in'] = $child_product_ids; } } return $query_vars; }All is good, products are not visible in products list. But terms of taxonomies which include only those products still are present in filter.
How can I hide from the filter taxonomy terms that only contain hidden products that are part of group products?
I need to hide products on my shop which are included in grouped products. I wrote function which does it:
add_filter('woof_products_query', 'woof_hide_grouped_products_from_category', 100); function woof_hide_grouped_products_from_category($query_vars) { $taxonomy = $query_vars['tax_query'][0]['taxonomy']; $term_slug = $query_vars['tax_query'][0]['terms'][0]; $term = get_term_by('slug', $term_slug, $taxonomy); $term_id = $term ? $term->term_id : 0; if (isset($taxonomy) && $taxonomy && isset($term_id) && $term_id) { $child_product_ids = get_grouped_children_ids($term_id, $taxonomy); if (!empty($child_product_ids)) { $query_vars['post__not_in'] = $child_product_ids; } } return $query_vars; }
All is good, products are not visible in products list. But terms of taxonomies which include only those products still are present in filter.
How can I hide from the filter taxonomy terms that only contain hidden products that are part of group products?
Quote from Pablo Borysenco on August 14, 2023, 13:11Hello
You can edit requests for counting - woof_dynamic_count_attr ( https://share.pluginus.net/image/i20230814141026.png )
Hello
You can edit requests for counting - woof_dynamic_count_attr ( https://share.pluginus.net/image/i20230814141026.png )
Quote from Andrii on August 15, 2023, 14:47Thank you! It works. But the plugin starts to work very slowly, because for each term there is a recalculation with post__not_in. And if I do not change the query, but exclude unnecessary elements already after receiving the result? Is there any hook for this case? And will it make the filter faster?
Thank you! It works. But the plugin starts to work very slowly, because for each term there is a recalculation with post__not_in. And if I do not change the query, but exclude unnecessary elements already after receiving the result? Is there any hook for this case? And will it make the filter faster?
Quote from Pablo Borysenco on August 16, 2023, 11:16Hello
Unfortunately not, because in any case, a dynamic recalculation will be needed.
Maybe there are woocommerce hooks, but unfortunately my filter plugin doesn't have such a hook
Hello
Unfortunately not, because in any case, a dynamic recalculation will be needed.
Maybe there are woocommerce hooks, but unfortunately my filter plugin doesn't have such a hook
Quote from Andrii on August 16, 2023, 12:44Understand. Thank you very much for the answers!
Understand. Thank you very much for the answers!
Quote from Andrii on August 16, 2023, 16:06Cancel post__not_in – he thinks for a very long time. I came up with another option, maybe someone will find it useful.
So, instead of searching for products that are part of a group product for each term separately, we store a special post_meta field for those products. We write the following synchronization when saving any product:
// Synchronize 'grouped_child' post_metas in products on product save add_action('save_post', 'synchronize_grouped_child_post_meta', 10, 3); function synchronize_grouped_child_post_meta($post_id, $post, $update) { $args = array( 'post_type' => 'product', 'posts_per_page' => -1, ); $products = get_posts($args); $grouped_products_ids = []; $simple_products_ids = []; foreach ($products as $product) { $parent_grouped_id = get_parent_grouped_id($product->ID); if ($parent_grouped_id) { $grouped_products_ids[] = $product->ID; } else { $simple_products_ids[] = $product->ID; } } foreach ($grouped_products_ids as $product_id) { if (!get_post_meta($product_id, 'grouped_child', true)) { update_post_meta($product_id, 'grouped_child', 'yes'); } } foreach ($simple_products_ids as $product_id) { if (get_post_meta($product_id, 'grouped_child', true)) { delete_post_meta($product_id, 'grouped_child'); } } }The get_parent_grouped_id function looks like this:
function get_parent_grouped_id($children_id) { global $wpdb; $results = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_children' AND meta_value LIKE '%$children_id%'"); // Will only return one product Id or false if there is zero or many return sizeof($results) == 1 ? reset($results) : false; }Now, instead of a complex condition to search for post__not_in, we add a simple meta-query to all three required hooks:
// Hide simple products from category page if they are in grouped product add_filter('woocommerce_product_query', 'hide_grouped_products_from_category', 99999); function hide_grouped_products_from_category($query) { if ( !is_admin() && $query->is_main_query() && $query->is_archive() ) { $meta_query = [ [ 'key' => 'grouped_child', 'compare' => 'not exists', ], ]; $query->set('meta_query', $meta_query); } } add_filter('woof_products_query', 'woof_hide_grouped_products_from_category', 100); add_filter('woof_dynamic_count_attr', 'woof_hide_grouped_products_from_category', 10); function woof_hide_grouped_products_from_category($query_vars) { $query_vars['meta_query'] = [ [ 'key' => 'grouped_child', 'compare' => 'not exists', ], ]; return $query_vars; }It works! Thanks for the tips that led to the correct decision!
Cancel post__not_in – he thinks for a very long time. I came up with another option, maybe someone will find it useful.
So, instead of searching for products that are part of a group product for each term separately, we store a special post_meta field for those products. We write the following synchronization when saving any product:
// Synchronize 'grouped_child' post_metas in products on product save add_action('save_post', 'synchronize_grouped_child_post_meta', 10, 3); function synchronize_grouped_child_post_meta($post_id, $post, $update) { $args = array( 'post_type' => 'product', 'posts_per_page' => -1, ); $products = get_posts($args); $grouped_products_ids = []; $simple_products_ids = []; foreach ($products as $product) { $parent_grouped_id = get_parent_grouped_id($product->ID); if ($parent_grouped_id) { $grouped_products_ids[] = $product->ID; } else { $simple_products_ids[] = $product->ID; } } foreach ($grouped_products_ids as $product_id) { if (!get_post_meta($product_id, 'grouped_child', true)) { update_post_meta($product_id, 'grouped_child', 'yes'); } } foreach ($simple_products_ids as $product_id) { if (get_post_meta($product_id, 'grouped_child', true)) { delete_post_meta($product_id, 'grouped_child'); } } }
The get_parent_grouped_id function looks like this:
function get_parent_grouped_id($children_id) { global $wpdb; $results = $wpdb->get_col("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_children' AND meta_value LIKE '%$children_id%'"); // Will only return one product Id or false if there is zero or many return sizeof($results) == 1 ? reset($results) : false; }
Now, instead of a complex condition to search for post__not_in, we add a simple meta-query to all three required hooks:
// Hide simple products from category page if they are in grouped product add_filter('woocommerce_product_query', 'hide_grouped_products_from_category', 99999); function hide_grouped_products_from_category($query) { if ( !is_admin() && $query->is_main_query() && $query->is_archive() ) { $meta_query = [ [ 'key' => 'grouped_child', 'compare' => 'not exists', ], ]; $query->set('meta_query', $meta_query); } } add_filter('woof_products_query', 'woof_hide_grouped_products_from_category', 100); add_filter('woof_dynamic_count_attr', 'woof_hide_grouped_products_from_category', 10); function woof_hide_grouped_products_from_category($query_vars) { $query_vars['meta_query'] = [ [ 'key' => 'grouped_child', 'compare' => 'not exists', ], ]; return $query_vars; }
It works! Thanks for the tips that led to the correct decision!
Quote from Pablo Borysenco on August 17, 2023, 10:54Hello
Great! Thank you for your cooperation
Hello
Great! Thank you for your cooperation