简体   繁体   中英

Hide quantity fields in cart for specific products in Woocommerce 3

I Would like to hide the quantity field from one specific product category in cart page.

The "Sold Individually" doesn't help me because I have a plugin that must be disabled to work.

Is it possible? Any track will be appreciated.

The following code will hide the product quantity field on cart page:

1) for a specific product category (that you will define in this code) :

add_filter( 'woocommerce_quantity_input_args', 'hide_quantity_input_field', 20, 2 );
function hide_quantity_input_field( $args, $product ) {
    // Here set your product categories in the array (can be either an ID, a slug, a name or an array)
    $categories = array('t-shirts','shoes');

    // Handling product variation
    $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only on cart page for a specific product category
    if( is_cart() && has_term( $categories, 'product_cat', $the_id ) ){
        $input_value = $args['input_value'];
        $args['min_value'] = $args['max_value'] = $input_value;
    }
    return $args;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.
It will also handle product variations added to cart.

2) for specific product IDs (that you will define in this code) :

add_filter( 'woocommerce_quantity_input_args', 'hide_quantity_input_field', 20, 2 );
function hide_quantity_input_field( $args, $product ) {
    // Here set your product IDs in the array
    $product_ids = array(37,40,70);

    // Handling product variation
    $the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only on cart page for a specific product category
    if( is_cart() && in_array( $the_id, $product_ids ) ){
        $input_value = $args['input_value'];
        $args['min_value'] = $args['max_value'] = $input_value;
    }
    return $args;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.
It will also handle product variations added to cart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM