Search code examples
phpwordpresswoocommercehook-woocommercewoocommerce-subscriptions

Remove particular variation from dropdown in WooCommerce frontend


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?


Solution

  • 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)