In add-to-cart/variable.php template file, we find foreach ( $attributes as $attribute_name => $options )
. However, the intention is to hide 1 attribute, so let's see where these are passed to the template file.
In includes/wc-template-functions.php, we can see that the template file is called and an array is passed with some options. One of these options is available_variations' => $get_variations ? $product->get_available_variations()
The get_available_variations()
function is then found in includes/class-wc-product-variable.php which in turn $variation_ids = $this->get_children();
contains.
The get_children()
function can then be found in includes/class-wc-product-variable.php, which contains apply_filters( 'woocommerce_get_children', $this->children, $this, false );
And that filter hook can be used to remove one or more childIDs (variantions)
So you get:
function filter_woocommerce_get_children( $children, $product, $false ) {
// NOT backend
if ( is_admin() ) return $children;
// Variation ID
$variation_id = 4171;
// Delete by value: Searches the array for a given value and returns the first corresponding key if successful
if ( ( $key = array_search( $variation_id, $children ) ) !== false ) {
unset( $children[$key] );
}
return $children;
}
add_filter( 'woocommerce_get_children', 'filter_woocommerce_get_children', 10, 3 );
If you want to apply it for multiple variantion IDs, use:
function filter_woocommerce_get_children( $children, $product, $false ) {
// NOT backend
if ( is_admin() ) return $children;
// Variation IDs, multiple IDs can be entered, separated by a comma
$variation_ids = array( 4171, 36, 38 );
// Loop through variation IDs
foreach ( $variation_ids as $variation_id ) {
// Delete by value: Searches the array for a given value and returns the first corresponding key if successful
if ( ( $key = array_search( $variation_id, $children ) ) !== false ) {
unset( $children[$key] );
}
}
return $children;
}
add_filter( 'woocommerce_get_children', 'filter_woocommerce_get_children', 10, 3 );
Used in this answer: PHP array delete by value (not key)