user4752139
user4752139

Reputation:

Remove Woocommerce cart quantity selector from cart page

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 );

Upvotes: 5

Views: 5899

Answers (4)

backups
backups

Reputation: 577

The quantity increment php file has 2 action calls before and after the quatity field.

Check for the sold individually in the action calls:

            function woocommerce_after_quantity_input_field() {
               global $product;
               if ( !$product->is_sold_individually() ) 
                   //add your minus icon spinner html code here;
            }


            function woocommerce_before_quantity_input_field() {
               global $product;
               if ( !$product->is_sold_individually() ) 
                   //add your plus icon spinner html code here;
            }
            

Reported at woocommerce https://github.com/woocommerce/woocommerce/issues/40923

Upvotes: 0

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

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

Upvotes: 9

Sarathlal N
Sarathlal N

Reputation: 899

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 );

Upvotes: 0

Giuls
Giuls

Reputation: 580

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 );

Upvotes: 1

Related Questions