Hide variable product dropdown that has a unique variation selected by default in Woocommerce

后端 未结 1 742
一向
一向 2021-01-15 19:05

I have a unique issue. I am using a plugin where they are not able to support the request. I need to split out variations into separate items, but if I copy and paste and t

相关标签:
1条回答
  • 2021-01-15 19:36

    IMPORTANT: The code only works when the unique variation is selected as default form value:

    The following code will hide from variable products that have only one variation enabled and selected by default, the attribute dropdown and the selected variation price:

    add_action( 'woocommerce_before_add_to_cart_form', 'single_unique_variation_ui', 10 );
    function single_unique_variation_ui(){
        global $product;
    
        if( ! $product->is_type('variable') )
            return; // Only variable products
    
        $available_variations = $product->get_available_variations(); // Get available variations
        $default_attributes   = $product->get_default_attributes();   // Get default attributes
    
        // Only for a unique selected variation by default
        if( ! ( sizeof($available_variations) == 1 && sizeof($default_attributes) == 1 ) )
            return;
    
        // Get the unique variation
        $variation = reset($available_variations);
    
        // Loop through
        if( reset($variation['attributes']) == reset($default_attributes) ) :
        // Styles
        ?>
        <style>
            div.woocommerce-variation-price, table.variations { display:none; }
        </style>
        <?php
        endif;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Without the code (the normal woocommerce behavior):

    With the code (that hide the attribute dropdown and the price):

    It will give you the same UI than simple products

    0 讨论(0)
提交回复
热议问题