Last active
June 27, 2025 15:23
-
-
Save shameemreza/4f0eb450be3302e0e03230fd8efa4f28 to your computer and use it in GitHub Desktop.
Fixes the `product_brand_thumbnails` shortcode from the WooCommerce Brands when `show_empty="false"` is used. Prevents double filtering of empty brands in WooCommerce 9.9.4+ by replacing the shortcode handler with a patched version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Fix for WooCommerce product_brand_thumbnails shortcode | |
| * | |
| * This snippet fixes an issue in the WooCommerce Brands where using | |
| * show_empty="false" parameter breaks the shortcode in WooCommerce versions newer than 9.9.3. | |
| * | |
| * The issue occurs because of double filtering of empty brands in the output_product_brand_thumbnails function. | |
| * | |
| */ | |
| if (!function_exists('fix_wc_brands_thumbnails_shortcode')) { | |
| /** | |
| * Fix the product_brand_thumbnails shortcode by replacing the original function | |
| * with a fixed version that doesn't double-filter empty brands. | |
| */ | |
| function fix_wc_brands_shortcode() { | |
| // Only run if WC_Brands class exists | |
| if (!class_exists('WC_Brands')) { | |
| return; | |
| } | |
| // Remove the original shortcode | |
| remove_shortcode('product_brand_thumbnails'); | |
| // Add our fixed version of the shortcode | |
| add_shortcode('product_brand_thumbnails', 'fixed_output_product_brand_thumbnails'); | |
| } | |
| add_action('init', 'fix_wc_brands_shortcode', 20); | |
| /** | |
| * Fixed version of the output_product_brand_thumbnails function | |
| * | |
| * This version doesn't apply the remove_terms_with_empty_products filter when hide_empty is already true, | |
| * which prevents double filtering of empty brands. | |
| * | |
| * @param array $atts Shortcode attributes | |
| * @return string HTML output | |
| */ | |
| function fixed_output_product_brand_thumbnails($atts) { | |
| $args = shortcode_atts( | |
| array( | |
| 'show_empty' => true, | |
| 'columns' => 4, | |
| 'hide_empty' => 0, | |
| 'orderby' => 'name', | |
| 'exclude' => '', | |
| 'number' => '', | |
| 'fluid_columns' => false, | |
| ), | |
| $atts | |
| ); | |
| $exclude = array_map('intval', explode(',', $args['exclude'])); | |
| $order = 'name' === $args['orderby'] ? 'asc' : 'desc'; | |
| // Convert show_empty to hide_empty | |
| if ('true' === $args['show_empty']) { | |
| $hide_empty = false; | |
| } else { | |
| $hide_empty = true; | |
| } | |
| // Get terms with hide_empty parameter | |
| $brands = get_terms( | |
| 'product_brand', | |
| array( | |
| 'hide_empty' => $hide_empty, | |
| 'orderby' => $args['orderby'], | |
| 'exclude' => $exclude, | |
| 'number' => $args['number'], | |
| 'order' => $order, | |
| ) | |
| ); | |
| if (!$brands) { | |
| return; | |
| } | |
| // No need for additional filtering - the hide_empty parameter in get_terms already did this | |
| // This is the key fix - removing the double filtering | |
| ob_start(); | |
| wc_get_template( | |
| 'widgets/brand-thumbnails.php', | |
| array( | |
| 'brands' => $brands, | |
| 'columns' => is_numeric($args['columns']) ? intval($args['columns']) : 4, | |
| 'fluid_columns' => wp_validate_boolean($args['fluid_columns']), | |
| ), | |
| 'woocommerce', | |
| WC()->plugin_path() . '/templates/brands/' | |
| ); | |
| return ob_get_clean(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment