I have a WooCommerce site and need product tags to be hidden from the product tag cloud when a product tag archive page has 0 'in stock' products available.
The code below shows my current progress and I will update as more progress is made. I believe this code could be adapted to run a check and remove any results which have a 'show count' value less than 1 which would answer this question. Additionally the hidden results will need to 302 redirect to the top level 'shop' page whilst it is temporarily hidden:
/* TURN PRODUCT TAG CLOUD INTO ALPHABETICAL LIST WITH TAG TOTALS COUNT VISIBLE */
function woocommerce_product_tag_cloud_widget_filter($args) {
$args = array(
'smallest' => 14,
'largest' => 14,
'format' => 'list',
'taxonomy' => 'product_tag',
'unit' => 'px',
'show_count' => 1,
'number' => 0,
);
echo "<div style='padding-left: 20px;'>";
return $args;
echo "</div>";
}
add_filter('woocommerce_product_tag_cloud_widget_args', 'woocommerce_product_tag_cloud_widget_filter');
I also have this code which might be used as a second approach. The notes show what is missing:
/* REMOVE PRODUCT TAGS FROM THE PRODUCT TAG CLOUD WHEN THEY HAVE LESS THAN 1 IN-STOCK RESULTS */
function filter_woocommerce_product_tag_cloud_widget_args( $array ) {
$c = // check to see how many in stock products a product tag archive has
if($c <= 1){
// show array excluding empty product tag archives
}else{
return $array;
}
}
add_filter( 'woocommerce_product_tag_cloud_widget_args', 'filter_woocommerce_product_tag_cloud_widget_args', 10, 1 );
I have the following resources to help, however my lack of PHP knowledge means I am unable to put these various pieces of code into a single function to achieve my desired outcome.
I have this piece of code from How to remove empty product tags from the tag cloud on woocommerce answer code to my previous question which hides all empty product tags from the tag cloud (but it doesn't factor in stock availability and so will still show product tags with 0 'in stock' products).
I have also some others pieces of code below which may or may not help with a final solution.
This code hides the entire product tag cloud.
add_action( 'widgets_init', 'misha_remove_product_tag_cloud_widget' );
function misha_remove_product_tag_cloud_widget(){
unregister_widget('WC_Widget_Product_Tag_Cloud');
}
This code hides all categories which have 0 'in stock' products.
add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
global $wpdb;
$nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
foreach ( $items as $key => $item ) {
if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
unset( $items[$key] );
}
}
return $items;
}
I have tried to put various elements of the above pieces of code together to create a function that achieves my goal, but my lack of ability means I have not got anything to correctly work and therefore I need additional help.