Last active
February 24, 2025 17:22
-
-
Save woogists/308135d3c6dece136b1531d8b36a3411 to your computer and use it in GitHub Desktop.
Hide attributes which match no products when option filtering is enabled on a component of a composite product
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
<?php | |
/* | |
When you enable the Options Filtering option in a component, a field shows up where you can select some product attributes. | |
The selected attributes and their terms are shown on the front-end to be used as filters. | |
Composite Products does not perform any checks to make sure that at least one product exists for each term. | |
This is because enforcing these checks would have a significant performance impact on the page. | |
Use this snippet to automatically remove any terms that do not have any matching products. | |
*/ | |
add_filter( 'woocommerce_composite_component_filters', 'sw_cp_disable_empty_filters', 10, 3 ); | |
function sw_cp_disable_empty_filters ( $filters, $component_id, $composite ) { | |
foreach ( $filters as $key => $value ) { | |
if ( 'Color' == $value[ 'filter_name' ] ) { | |
unset( $filters[ $key ][ 'filter_options' ][26] ); | |
} | |
} | |
return $filters; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment