phpwordpresswoocommerceproductproduct-quantity

Remove quantity field everywher for a specific product in WooCommerce


II need to remove the quantity field for a specific variable product on single product Page, cart Page and everywhere else.

I used this:

add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
function wc_remove_all_quantity_fields( $return, $product ) 
{
    return( true );
}

This remove the quantity field for all products.

How to target a specific variable product, to remove the quantity field everywhere?


Solution

  • Try the following, adding your desired product ID(s) below (work for simple, variable and variations product type):

    add_filter( 'woocommerce_is_sold_individually', 'remove_all_qty_fields_on_specific_product', 10, 2 );
    function remove_all_qty_fields_on_specific_product( $sold_individually, $product ) {
        // Here define the desired product Id(s)
        $targeted_product_ids = array( 90 );
        if ( count(array_intersect([$product->get_id(), $product->get_parent_id()], $targeted_product_ids) ) > 0 ) {
            return true;
        }
        return $sold_individually;
    }
    

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