I need to hide the "quantity" field (where one enters the quantity before adding to cart) in Woocommerce on the product detail page, and only show the "add-to-cart"-button, which would then put the quantity of 1 in the cart. The reason is because I gather the quantity based on a Gravity Forms.
12 Answers
- Edit your product.
- Click "Inventory".
- Check the box that says "Sold Individually"
-
This is best for a product by product basis. Otherwise, filtering
woocommerce_is_sold_individually
works best for blanket application to all products. Dec 7, 2015 at 6:35
The safest way is to use WordPress builtin hook or filter
/**
* @desc Remove in all product type
*/
function wc_remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'wc_remove_all_quantity_fields', 10, 2 );
You can also remove Quantity selector in other product type, you can found our more here http://www.sutanaryan.com/how-to-remove-product-quantity-selectors-woocommerce/
Please be warned: using this option effictively makes it impossible to have a product more than once in your shopping cart. Subsequently clicking "Add to cart" will trigger a warning that this product can only be in your cart once. This might not be desirable for everyone.
-
3This is by far the best answer here and should be accepted as the answer to the question. Sep 18, 2014 at 13:18
-
agree! it overwrites the setting in product data tab -> inventory. be aware of that.– nicmareNov 15, 2014 at 11:19
-
@Reinoud van Santen I dont think so - it relies on a hook that might change also
wc_remove_all_quantity_fields
seems like it could remove ANY quantity field..– jave.webMar 11, 2015 at 23:20 -
Please be warned: using this option effictively makes it impossible to have a product more than once in your shopping cart. Subsequently clicking "Add to cart" will trigger a warning that this product can only be in your cart once. This might not be desirable for everyone. Apr 2, 2015 at 11:03
-
2This can be simplified to:
add_filter( 'woocommerce_is_sold_individually', '__return_true' );
– DylanSep 22, 2016 at 22:56
There is no need to use a plugin or edit function.php just check the option shown in the image above.
-
I love it when I think something is going to be complicated, and then I run into an answer like this and it's just easy. Sep 19, 2018 at 23:10
-
1Curiously enough, I have this and yet the quantity is still shown in the checkout.– j4hangirJan 16, 2019 at 18:24
There is a free plugin to remove the quantity selectors that might work for you. http://wordpress.org/extend/plugins/woocommerce-remove-quantity-fields/
I found a simple way to do it just in product single page, and keeping the quantity counter in cart. Just put the following code in functions.php
add_action( 'wp_head', 'quantity_wp_head' );
function quantity_wp_head() {
if ( is_product() ) {
?>
<style type="text/css">.quantity, .buttons_added { width:0; height:0; display: none; visibility: hidden; }</style>
<?php
} else { ?>
<?php }
}
You can check the woocommerce documentation here: http://docs.woothemes.com/document/remov-product-content-based-on-category/
-
sorry, but this solution is just ugly. insert a style tag on pages that are single product..? why not use the default wordpress style.css with a decent selector in the first place?!– honk31Nov 15, 2017 at 10:33
There´s no need for plugin, for example you can hide it using css. But woocommerce let you sell just 1 product, with no choise to add more of same item to cart. Take a look on woocommerce->settings. It´s all there.
-
2I dispise this notion of hiding functionality in Wordpress via CSS. CSS is for presentation. Jan 5, 2016 at 6:39
-
This should not be done using css - the woocommerce product settings do this Oct 29, 2017 at 6:21
-
2"But woocommerce let you sell just 1 product, with no choise to add more of same item to cart" Did you read that part?– rgdesignOct 30, 2017 at 15:20
You can also use the woocommerce_quantity_input_min
and woocommerce_quantity_input_max
hooks by setting both quantities to 1.
In fact, in the /woocommerce/global/quantity-input.php
template, the quantity field will be automatically hidden if min and max have the same value.
// hides the quantity field on the product page
add_filter( 'woocommerce_quantity_input_min', 'hide_woocommerce_quantity_input', 10, 2 );
add_filter( 'woocommerce_quantity_input_max', 'hide_woocommerce_quantity_input', 10, 2 );
function hide_woocommerce_quantity_input( $quantity, $product ) {
// only on the product page
if ( ! is_product() ) {
return $quantity;
}
return 1;
}
The code has been tested and works. Add it to your active theme's functions.php.
The template you need to edit is single-product/add-to-cart/variation-add-to-cart-button.php
.
So you can simply copy this template in your own theme and edit it to remove the quantity field. It would become something like:
<?php
/**
* Single variation cart button
*
* @see http://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
There is an edge case that is not being handled by other answers to this question, the 'is sold individually' option of woocommerce effectively removes the quantity input but prevents the same product to be added multiple times to the cart
In a use case where you have a product with custom attributes where you don't want the quantity to be editable but you still want to allow a user to add the same product to the cart with different attributes, then the 'is sold individually' option will not work
What you need in this case is this filter
add_filter( 'woocommerce_cart_item_quantity', function ( $qty, $item_key, $item ) {
if ( ! empty( $item['custom_data'] ) ) { //Here check for your custom attribute
return sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $item_key );
}
return $qty;
}, 10, 3 );
Setting the product (or the store as a whole) to 'sold individually' as explained by the other answers here now works for me, but I also found another solution: Hiding the quantity field on a particular page ID. If anyone wants the field to show on certain pages and not others for whatever reason, consider this alternative:
.page-id-11111 .woocommerce .quantity .qty {
Display:None!important;
}
This will let people add multiple times to cart, and edit quantities at checkout if they do, while still hiding the quantity field on the product shortcode/page. More info here: Remove quantity field from Woocommerce, without preventing multiple cart additions? (I have reworked the site so this solution isn't necessary for me anymore, but this may help someone.)
It's very simple, in woocommerce\includes\abstracts\abstract-wc-product.php
, find abstract-wc-product.php
file in woocommerce
Find below code in page
$availability = sprintf( __( '%s in stock', 'woocommerce' ), $this->get_total_stock() );
replace this code with
$availability = sprintf( __( '%s in stock', 'woocommerce' ),'');
-
3