简体   繁体   中英

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

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

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

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

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