WooCommerce current category hide - wordpress

I have done all the child category shows in each parent category. Now I want the current or active category not to be displayed when I am on the category page.
$queried_object = get_queried_object();
$child_terms = get_term_children ( $queried_object->term_id, 'product_cat' );
$main_term = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;
$terms_arg = get_terms( 'product_cat', array(
'orderby' => 'name',
'hide_empty' => false,
'parent' => $main_term->term_id,
) );
if( !empty( $terms_arg ) && !is_wp_error( $terms_arg ) ){
foreach( $terms_arg as $display_term ){
printf(
'<div class="cat-list"><h3%s>%s</h3></div>',
($display_term->term_id == $queried_object->term_id) ? : '',
esc_url(get_term_link($display_term->term_id)),
$display_term->name,
);
}
}

try this
foreach( $terms_arg as $display_term ){
if( $display_term->term_id != $queried_object->term_id ) {
printf(
'<div class="cat-list"><h3>%s</h3></div>',
esc_url(get_term_link($display_term->term_id)),
$display_term->name,
);
}
}

According to me get_terms() is having the options of exclude under the argument parameter
Try with below code
$terms_arg = get_terms( 'product_cat', array(
'orderby' => 'name',
'hide_empty' => false,
'parent' => $main_term->term_id,
'exclude' => $display_term->term_id, //assuming that $display_term->term_id is getting current category id
) );

Related

Related Products display first same subcategory

Trying to display related products in WooCommerce first by same subcategory and when there are no products to show in that, display another products from parent category, avoiding being empty.
For example:
Fashion > Clothes > T-Shirts
When there are no t-shirts available to show, display another products from parent category (clothes).
Tried it but it is not working:
function wc_related_products_by_last_available_depth_term( $related_posts, $product_id, $args ) {
$product = wc_get_product( $product_id );
$terms = wp_get_post_terms( $product_id, 'product_cat' );
$hierarchy = array();
$cat_id = '';
// find the depth of terms
foreach ( $terms as $key => $term ) {
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
if( $ancestors && count( $ancestors ) > 1 ) {
$hierarchy[$term->term_id] = max($ancestors);
}elseif( $ancestors ) {
$hierarchy[$term->term_id] = $ancestors[0];
}
$cat_id = $term->term_id;
}
// if level of depth term available replace $cat_id
if( $hierarchy ){
$cat_id = max( array_keys( $hierarchy ) );
}
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'exclude' => array( $product_id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( $cat_id )
)
)
));
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'wc_related_products_by_last_available_depth_term', 99, 3 );

WooCommerce: disable AJAX reload on product page when selecting an attribute

in WooCommerce 4.2.0, when the user selects an attribute on the product page (i.e. product size), the AJAX reloads the "other products" in WooCommerce tabs. It's painful for the user to wait for this. How to disable this AJAX reload please ?
The code for the "other colors" tabs in my Storefront child-theme's functions.php:
/*ADD custom "other colors" tab */
add_filter( 'woocommerce_product_tabs', 'wpb_new_product_tab_othercolors' );
function wpb_new_product_tab_othercolors( $tabs ) {
// Add the new tab
global $post;
$othercolorsbool = get_post_meta( $post->ID, '_othercolorsbool', true );
// var_dump('$othercolorsbool :');
// var_dump($othercolorsbool);
if ( $othercolorsbool === "false" || $othercolorsbool === "" || $othercolorsbool === "0" ) {
unset( $tabs['othercolors_tab'] );
return $tabs;
} else {
if( get_locale() == 'fr_FR' ) {
$tabs['othercolors_tab'] = array(
'title' => __( 'AUTRES COULEURS', 'text-domain' ),
'priority' => 0,
'callback' => 'wpb_new_product_tab_content_othercolors'
);
} else {
$tabs['othercolors_tab'] = array(
'title' => __( 'OTHER COLORS', 'text-domain' ),
'priority' => 0,
'callback' => 'wpb_new_product_tab_content_othercolors'
);
}
return $tabs;
}
}
function wpb_new_product_tab_content_othercolors() {
global $post;
$title = $post->post_title;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
$splitted = product_name_split($title);
$productname = $splitted[0];
$productcolor = $splitted[1];
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_cat_id
),
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_cat_id
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
// The Loop
$r = 0;
while ( $the_query->have_posts() ) {
$r = $r + 1;
$the_query->the_post();
$currenttitle = get_the_title();
if ( strpos($currenttitle, $productname) !== false) { //if iteration product has the same name as the productname, then find its color: $productcolorbis
$splitted = product_name_split($currenttitle);
$productnamebis = $splitted[0];
$productcolorbis = $splitted[1];
if ( ($productcolorbis !== $productcolor) ) { //if iteration product color is different than productcolor, then go
wc_get_template_part( 'content', 'product' );
}
}
}
wp_reset_postdata();
}

Add a filter dropdown for specific product attribute in woocommerce admin product list

In Woocommerce, on the admin products list, I should need to filter products for a specific attribute.
Based on "Add a filter dropdown for product tags in woocommerce admin product list" answer code, I have made some changes to filter products for pa_proizvoditel product attribute:
add_action('restrict_manage_posts', 'product_attr_sorting');
function product_attr_sorting() {
global $typenow;
$taxonomy = 'pa_proizvoditel';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Attr {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_filter('parse_query', 'product_attr_sorting_query');
function product_attr_sorting_query($query) {
global $pagenow;
$taxonomy = 'pa_proizvoditel';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
But when you select a value and click on the "filter" button, the page is updated, but the products are not filtered (a complete list of all products).
Any help to solve this problem issue is appreciated.
Спасибо за помощь. Понял код работает (первый код пробовал в версии 2.5) на версиях woocommerce ниже 3 Вот код, который работает на версии выше 3 (проверено на 3.5+) Я не знаю, в чем дело, но у меня есть
function filter_proizvoditel_admin_sort( &$query )
{
if (
is_admin()
AND 'edit.php' === $GLOBALS['pagenow']
AND isset( $_GET['pa_proizvoditel'] )
AND '-1' != $_GET['pa_proizvoditel']
)
{
$query->query_vars['tax_query'] = array( array(
'taxonomy' => 'pa_proizvoditel'
,'field' => 'ID'
,'terms' => array( $_GET['pa_proizvoditel'] )
) );
}
}
add_filter( 'parse_query', 'filter_proizvoditel_admin_sort' );
function filter_proizvoditel_form()
{
$selected = isset($_GET['pa_proizvoditel']) ? $_GET['pa_proizvoditel'] : '';
wp_dropdown_categories( array(
'taxonomy' => 'pa_proizvoditel',
'hide_empty' => 0,
'name' => 'pa_proizvoditel',
'show_option_none' => 'Все производители',
'show_count' => false,
'hide_empty' => true,
'selected' => $selected
) );
}
add_action( 'restrict_manage_posts', 'filter_proizvoditel_form', 25 );
I have tested your version code and it works for any defined product attribute taxonomy. Now there is some minors mistakes like parse_query hook that is an action hook (but not a filter hook).
For example I use the product attribute taxonomy pa_color in the revisited code below:
add_action('restrict_manage_posts', 'product_attribute_sorting_dropdown');
function product_attribute_sorting_dropdown() {
global $typenow;
$taxonomy = 'pa_color';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Attribute {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
add_action('parse_query', 'product_attribute_sorting_query');
function product_attribute_sorting_query( $query ) {
global $pagenow;
$taxonomy = 'pa_color';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works on Woocommerce version 3+ (for sure).
I get the following Product Attribute dropdown:
Then when I filter with the blue (5) term, I get the correct number of filtered products:
Related: Add a filter dropdown for product tags in woocommerce admin product list
I changed your code a little bit and now this works 100%.
My product attribute slug is "dobavitelj_xml" but you have to append "pa_" so taxonomy slug in my case is "pa_dobavitelj_xml".
add_action('restrict_manage_posts', 'product_attribute_sorting_dropdown');
function product_attribute_sorting_dropdown() {
global $typenow;
$taxonomy = 'pa_dobavitelj_xml';
if ( $typenow == 'product' ) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("{$info_taxonomy->labels->name}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => false,
));
};
}
add_action('parse_query', 'product_attribute_sorting_query');
function product_attribute_sorting_query( $query ) {
global $pagenow;
$taxonomy = 'pa_dobavitelj_xml';
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($_GET[$taxonomy]) && is_numeric($_GET[$taxonomy]) && $_GET[$taxonomy] != 0) {
$tax_query = (array) $query->get('tax_query');
$term = get_term_by('id', $_GET[$taxonomy], $taxonomy);
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => array($term->slug),
'operator' => 'AND'
);
$query->set( 'tax_query', $tax_query );
}
}

Dynamically tax_query terms

To display all the categories name with checkbox from custom taxonomy called ct_category I have the following code:
$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
I would like to display content only from checked categories. I tried following that didn't work:
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $_POST['taxonomy_category']
)
)
);
$loop = new WP_Query( $args );
I can guess that the issue is in 'terms' => $_POST['taxonomy_category']. If name attribute taxonomy_category['.$term->term_id.'] can be displayed as array in 'terms' =>, the issue would be fixed. Spent plenty of times searching google but couldn't find any solution.
Here is the full code
<?php
function add_meta_box() {
add_meta_box( 'ct_metabox', 'CT', 'meta_box_content_output', 'cpt', 'normal' );
}
add_action( 'add_meta_boxes', 'add_meta_box' );
function meta_box_content_output ( $post ) {
wp_nonce_field( 'save_meta_box_data', 'meta_box_nonce' );
$taxonomy_category = get_post_meta( $post->ID, 'taxonomy_category', true );
function categories_checkbox(){
$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
}
}
<?
<ul>
<li>
<?php categories_checkbox() ?>
<li>
</ul>
<?php
}
function save_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['meta_box_nonce'], 'save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
$taxonomy_category_value = "";
if(isset($_POST["logo_taxonomy_category"])) {
$taxonomy_category_value = sanitize_text_field( $_POST["logo_taxonomy_category"] );
}
update_post_meta($post_id, "logo_taxonomy_category", $taxonomy_category_value);
}
add_action( 'save_post', 'save_meta_box_data' );
?>
<?php
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => $ct_category,
'field' => 'term_id',
'terms' => $taxonomy_category
)
)
);
$loop = new WP_Query( $args );
?>
Print the checkboxes as this
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category[]" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';
This will collect all the IDs in an array $_POST['taxonomy_category']
And then write the wp_query as below:
remove $ from taxonomy name and pass the value of terms as below
$args = array(
'post_type' => 'cpt',
'tax_query' => array(
array(
'taxonomy' => 'ct_category',
'field' => 'term_id',
'terms' => $_POST['taxonomy_category'],
'operator'=> 'IN'
)
)
);
$loop = new WP_Query( $args );

Querying posts from same categories / tags

I want to find all posts from same categories and same tags like a specific post in a plugin. So I do it actually separately by querying for tags and categories:
$taxonomy_arr = wp_get_post_tags( $this->post->ID, array( "fields" => "ids" ) );
add_filter( 'posts_where', array( $this, 'additional_filter' ) );
foreach ( $taxonomy_arr as $tag_id ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results,
'tag_id' => (int) $tag_id,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
) );
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_taxonomy_selection[] = (int) $post_obj->ID;
}
}
}
}
// get post categories
$category_array = get_the_category( $this->post->ID );
foreach ( $category_array as $category ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results*2,
'category' => $category->cat_ID,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
));
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_category_selection[] = (int) $post_obj->ID;
}
}
}
}
// combine post id's arrays
$all_posts = $local_taxonomy_selection + $local_category_selection;
It's working but is there a way to do it in one query?
This question already has answer on wordpress.stackexchange. So copying same code here.
https://wordpress.stackexchange.com/questions/4201/how-to-query-posts-by-category-and-tag
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;

Resources