5

I am trying to remove Woocommerce cart quantity selector from the cart page. I am using the quantity input field on my shop archive pages and it has applied it to the cart page. How can I remove it and not allow the user to change it?

I have tried the following with the code below, researched and found from official Woocommerce docs but it is doesnt apply the rule...

function wc_remove_quantity_field_from_cart() {

if ( is_cart() ) return true;

}

add_filter( 'woocommerce_is_sold_individually', 'wc_remove_quantity_field_from_cart', 10, 2 );

3 Answers3

8

there's a bigger problem in your code than just fixing it.

Instead use this:

add_filter( 'woocommerce_cart_item_quantity', 'wc_cart_item_quantity', 10, 3 );
function wc_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
    if( is_cart() ){
        $product_quantity = sprintf( '%2$s <input type="hidden" name="cart[%1$s][qty]" value="%2$s" />', $cart_item_key, $cart_item['quantity'] );
    }
    return $product_quantity;
}

this will change the select field into a hidden field. Thus the quantity is there correctly. Unlike changing the sold individually property which will make the quantity on the cart just 1.

reigelgallarde.me

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
0

You were just missing the $return and $product from your function...The below function will work otherwise with the built in hook.

function wc_remove_quantity_field_from_cart( $return, $product ) {

if ( is_cart() ) return true;

}

add_filter( 'woocommerce_is_sold_individually', 'wc_remove_quantity_field_from_cart', 10, 2 );
Giuls
  • 570
  • 11
  • 33
0

Can you try below code?

function wc_remove_all_quantity_fields( $return, $product ) {
    if(is_cart()){
        return true;
    }
}
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
Sarathlal N
  • 839
  • 5
  • 13