2

I am using WooCommerce and WooCommerce Subscriptions and its working as per my expectations.

Now I am creating a variable subscription product having multiple attributes like this.

enter image description here

enter image description here

enter image description here

Now I want to remove/hide particular item from dropdown hence I am trying to use below code / hook which I believe might help me to achieve.

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'hide_variations_for_mindesk_users');

function hide_variations_for_mindesk_users( $args ){        
    
   print_r($args);

    return $args;    
}

Now my question is, how can I remove or hide particular variation product from dropdown? Do I need to remove from variation id or from somewhere?


For example:

Here I want to remove/hide 2nd variation from dropdown which has variation id #4171 having "Monthly- Professional". This should work with single attribute as well.

Can anyone point me in the right direction to achieve this?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Mittul At TechnoBrave
  • 1,142
  • 3
  • 25
  • 70

1 Answers1

1

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)

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Thanks for your answer .. can you please elaborate, what is `$key` here ? How can I get and pass it to check in my `if` condition ? – Mittul At TechnoBrave Jun 03 '21 at 09:09
  • @MittulAtTechnoBrave An [array](https://www.php.net/manual/en/language.types.array.php) (`$children` in this case) consists of 1 or more a key(s) with corresponding value(s). Normally when you use [unset()](https://www.php.net/manual/en/function.unset.php) on an array, you do this based on the key. However, because the `$variation_id` in the array `$children` is a value, not a key we use the if condition. If the `$variation_id` exists in the array, unset it. `$key` should not be modified in this answer, just the variation id if desired `$variation_id =` **4171** `;` – 7uc1f3r Jun 03 '21 at 09:24