Remove Woocommerce cart quantity selector from cart page

拜拜、爱过 提交于 2019-12-10 23:54:59

问题


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

回答1:


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



回答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.




回答3:


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


来源:https://stackoverflow.com/questions/50562595/remove-woocommerce-cart-quantity-selector-from-cart-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!