phpjquerywoocommercecartproduct-variations

Hide quantity button for virtual product variations in WooCommerce


I'm an author, and I have my books set up as variable products in my WooCommerce store. Each book/variable product has a paperback, e-book, and audiobook variant. I want to leave the quantity button for paperbacks and remove it for the digital items, but for the life of my I can't figure out how to do that. Any info I can find seems to be a blanket option of dealing with product-type-variable, and if I understand this correctly, using that would remove the button from all 3 variants.

I started off thinking this would be as simple as adding a class and display:none to my CSS, but that doesn't seem to be the case, mainly because I can't seem to be able to find the class for each variant when inspecting my pages.

I've tried this: Set quantity minimum, maximum and step at product level in Woocommerce but it limits quantities on all variants of the variable product, not selected variants.

I also tried: Woocomerce - Update max quantity for product variation but again, this changes quantities for all variants of a variable product.

I do NOT want to limit quantities of variant paperbacks. I only want to limit quantities of variant ebooks and variant audiobooks. Hopefully, that's doable!


Solution

  • This is doable in a simple way with a bit of PHP and JavaScript.

    First, you need to set up The "ebook" and "audiobook" variations as virtual products:

    Admin-variable product

    Then the following code will (for virtual variations):

    The code:

    // Product page: Set max quantity for virtual product variation
    add_filter( 'woocommerce_available_variation', 'wc_available_variation_max_qty', 10, 3 );
    function wc_available_variation_max_qty( $data_variation, $product, $variation ) {
        if ( $data_variation['is_virtual'] ) {
            $data_variation['max_qty'] = 1;
        }
        return $data_variation;
    }
    
    // Product page: Hide quantity field for virtual product variation
    add_action('woocommerce_single_variation', 'variable_product_jquery_script', 5);
    function variable_product_jquery_script() {
        // Enqueued JavaScript
        wc_enqueue_js( "$('form.variations_form').on('show_variation', function(event, data){ 
            if( data.is_virtual ) {
                $('div.quantity').hide();
            } else {
                $('div.quantity').show();
            }
        });" );
    }
    
    // Cart page: Hide quantity field for virtual product variation
    add_filter( 'woocommerce_quantity_input_args', 'filter_wc_quantity_input_args', 10, 2 );
    function filter_wc_quantity_input_args( $args, $product ) {
        if ( $product->is_virtual() ) {
            $args['min_value'] = $args['max_value'] = 1;
        }
        return $args;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin).

    In single variable product page (physical product variation):

    physical product variation

    In single variable product page (virtual product variation):

    virtual product variation

    In cart page:

    enter image description here

    Based on: