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
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.
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 }
}
You can check the woocommerce documentation here: http://docs.woothemes.com/document/remov-product-content-based-on-category/
There is a free plugin to remove the quantity selectors that might work for you. http://wordpress.org/extend/plugins/woocommerce-remove-quantity-fields/
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.)
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.
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' ),'');