Reputation: 31
I’ve usually been able to solve each of my WP demands by myself but this one proved to be a bit tricky.
The thing is, I need to hide certain categories from the Product Category Widget based on the category I’m in.
Example: When I’m in category A, I want to show categories A, B, C, D but not E. However, when I’m in category B, I want to show categories A, B, C, D and E as well. Then, let’s say I’m in category C and I want to only show categories C and D.
I found the following code
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array('15');
return $cat_args;
}
What I want to do with this code is to add multiple lines where I would define that when, for example, I open category ID 1 don’t show IDs 2,3,4. Then the next line would define what IDs not to show for ID 2, next for let’s say ID 4, and so on.
/* EDIT An amazing stackoverflow user helped me by creating the code you can see in his post below. However, as we found out, the example I posted above only works for the built-in WP Product Categories Widget.
That led me to find out that the Product Categories Widget I use is custom-built with the theme I use and apparently, it uses this code to work:
if ( !function_exists( 'getbowtied_megamenu_output_shop_icons' )):
/**
* Build the layout for the "Shop Icons" type megamenu
*
* @param int $theID id of the menu item
*
* @return html
*/
function getbowtied_megamenu_output_shop_icons( $theID, $cat= false) {
if ( !GETBOWTIED_WOOCOMMERCE_IS_ACTIVE ) return;
$cat_list = GBT_Opt::getOption('product_categories_icons_megamenu_' . $theID );
ob_start();
if ($cat !== true):
$args= array( 'taxonomy' => 'product_cat','hide_empty' => 0, 'menu_order' => 'asc', 'parent' =>0, 'include' => $cat_list );
else:
$args= array( 'taxonomy' => 'product_cat','hide_empty' => 0, 'menu_order' => 'asc', 'parent' =>$theID, 'include' => $cat_list );
endif;
$cats = get_terms( $args );
if ( is_array($cat_list)):
$unsorted = array();
$sorted = array();
foreach ($cats as $v) {
$unsorted[$v->term_id] = $v;
}
foreach ($cat_list as $v) {
if (isset($unsorted[apply_filters( 'wpml_object_id', $v, 'category', TRUE)]))
$sorted[] = $unsorted[apply_filters( 'wpml_object_id', $v, 'category', TRUE)];
}
else:
$sorted = $cats;
$sorted = array_slice($cats, 0, 8);
endif;
echo '<div class="megamenu_icon_list">';
foreach( $sorted as $cat ) {
$icon_type = get_term_meta( $cat->term_id, 'getbowtied_icon_type', true );
if ( $icon_type == 'custom_icon' ) {
$thumbnail_id = get_term_meta( $cat->term_id, 'icon_img_id', true );
if ($thumbnail_id)
$icon = wp_get_attachment_thumb_url( $thumbnail_id );
else
$icon = wc_placeholder_img_src();
// Prevent esc_url from breaking spaces in urls for image embeds
// Ref: https://core.trac.wordpress.org/ticket/23605
$icon = str_replace( ' ', '%20', $icon );
echo '<a href="'.esc_url( get_term_link( $cat->term_id ) ).'"><img src="'. $icon .'" alt="'. $cat->name .'" /><span>'. $cat->name .'</span></a>';
} else {
$icon = get_term_meta( $cat->term_id, 'icon_id', true );
if (!$icon) {
$icon = 'thehanger-icons-alignment_align-all-1';
}
echo '<a href="'.esc_url( get_term_link( $cat->term_id ) ).'"><i class="'. $icon .'"></i><span>'. $cat->name .'</span></a>';
}
}
echo '</div>';
$output = ob_get_contents();
ob_end_clean();
return $output;
}
endif;
*/
Please, can anyone point me in the right direction as to how approach this task?
Thank you in advance.
Upvotes: 1
Views: 986
Reputation: 29650
There are several ways, for example in the following way
function woo_product_cat_widget_args( $cat_args ) {
if ( is_product_category() ) {
// Get current category id
$current_cat_id = get_queried_object_id();
// Category id = ?, exclude ids ??
$cat_id_1_exclude_ids = array( 1, 2, 3, 4 );
$cat_id_2_exclude_ids = array( 1, 2, 3, 4, 5 );
$cat_id_15_exclude_ids = array( 16, 18 );
if ( !empty( $current_cat_id ) ) {
// Excludes ID's based on current category id
// Extra check that the variable name (array) exists
$exclude_ids = isset( ${'cat_id_' . $current_cat_id . '_exclude_ids'} ) ? ${'cat_id_' . $current_cat_id . '_exclude_ids'} : '';
if ( $exclude_ids ) {
$cat_args['exclude'] = $exclude_ids;
}
}
}
return $cat_args;
}
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args', 10, 1 );
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'woo_product_cat_widget_args', 10, 1 );
Upvotes: 2