如何在woocommerce中禁用产品详细信息页面中的数量字段?

如何在woocommerce中禁用产品详细信息页面中的数量字段?,woocommerce,Woocommerce,我需要在产品详细信息页面上隐藏Woocommerce中的“数量”字段(在添加到购物车之前输入数量),并且只显示“添加到购物车”按钮,然后将数量1放入购物车。原因是我根据重力表单收集数量。有一个免费插件,可以删除可能适合您的数量选择器。 不需要插件,例如,您可以使用css隐藏插件。但woocommerce只允许您销售一种产品,没有选择将更多相同的商品添加到购物车中。查看woocommerce->设置。一切都在那里。 编辑您的产品 单击“库存” 勾选“单独出售”的方框 最安全的方法是使用WordPr

我需要在产品详细信息页面上隐藏Woocommerce中的“数量”字段(在添加到购物车之前输入数量),并且只显示“添加到购物车”按钮,然后将数量1放入购物车。原因是我根据重力表单收集数量。

有一个免费插件,可以删除可能适合您的数量选择器。
不需要插件,例如,您可以使用css隐藏插件。但woocommerce只允许您销售一种产品,没有选择将更多相同的商品添加到购物车中。查看woocommerce->设置。一切都在那里。

  • 编辑您的产品
  • 单击“库存”
  • 勾选“单独出售”的方框

最安全的方法是使用WordPress内置挂钩或过滤器

/**
 * @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 );
您也可以删除其他产品类型中的数量选择器,您可以在此处找到我们的更多信息


请注意:如果有效地使用此选项,则不可能在您的购物车中多次使用产品。随后单击“添加到购物车”将触发一个警告,该产品只能在您的购物车中出现一次。这可能不是每个人都想要的。

我找到了一个简单的方法,只需在产品单页中完成,并将数量计数器保留在购物车中。只需将以下代码放入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>
add_action('wp_head','quantity_wp_head');
功能数量\u wp\u头(){
if(is_product()){
?>
.quantity、.buttons_添加了{宽度:0;高度:0;显示:无;可见性:隐藏;}

您需要编辑的模板是
单个产品/添加到购物车/变体添加到购物车按钮。php

因此,您只需在自己的主题中复制此模板,然后对其进行编辑以删除“数量”字段。 它会变成这样:

<?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>


很简单,在
woocommerce\includes\abstracts\abstract wc product.php
中,在woocommerce中查找
abstract wc product.php
文件

在第页找到下面的代码

$availability = sprintf( __( '%s in stock', 'woocommerce' ), $this->get_total_stock() );
将此代码替换为

$availability = sprintf( __( '%s in stock', 'woocommerce' ),'');


不需要使用插件或编辑函数。php只需检查上图中显示的选项。

此问题的其他答案没有处理边缘情况, woocommerce的“单独销售”选项有效地删除了数量输入,但防止同一产品多次添加到购物车中

如果您有一个具有自定义属性的产品,您不希望数量可编辑,但您仍然希望允许用户使用不同属性将同一产品添加到购物车,则“单独销售”选项将不起作用

在这种情况下,您需要的是这个过滤器

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 );
添加过滤器('woocommerce\u cart\u item\u quantity',函数($quantity,$item\u key,$item){
如果(!empty($item['custom_data']){//在此处检查您的自定义属性
返回sprintf('1',$item_key);
}
返回$qty;
}, 10, 3 );

将产品(或整个商店)设置为“单独出售”,如这里的其他答案所解释的,现在我也在工作,但我也发现了另一种解决方案:将数量字段隐藏在特定的页面ID上。如果有人希望该字段在某些页面上显示,而不是由于其他原因显示,请考虑此备选方案:

.page-id-11111 .woocommerce .quantity .qty {
    Display:None!important;
}
这将允许人们多次添加到购物车中,并在结帐时编辑数量(如果有),同时仍然隐藏产品快捷码/页面上的数量字段。此处的详细信息:(我已重新修改了网站,因此不再需要此解决方案,但这可能会对某些人有所帮助。)

您还可以通过将两个数量设置为1来使用和挂钩

事实上,在
/woocommerce/global/quantity input.php
模板中,如果minmax具有相同的值,则数量字段将自动隐藏

// 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;
}

代码已经过测试并运行正常。请将其添加到活动主题的functions.php中。

这是迄今为止最好的答案,应该被接受为问题的答案。同意!它覆盖了“产品数据”选项卡>“库存”中的设置。请注意。@Reinoud van Santen我不这么认为-它依赖的钩子可能也会发生变化_删除\u所有\u数量\u字段
似乎可以删除任何数量字段。请注意:使用此选项将无法在您的购物车中多次使用产品。随后单击“添加到购物车”将触发一个警告,说明此产品只能在您的购物车中出现一次。这可能不是每个人都需要的。这可以简化为:
add\u filter('woocommerce\u是单独销售的','uuuu return\u true'))
这是针对每个产品的最佳选择。否则,过滤
woocommerce是单独出售的
最适合于所有产品的全面应用。我反对通过CSS在Wordpress中隐藏功能的概念。CSS是用于表示的。这不应该使用CSS来完成-woocommerce产品设置可以做到这一点“但woocommerce只允许您销售一种产品,您没有选择向购物车添加更多相同的商品"你读过那部分了吗?抱歉,但这个解决方案太难看了。在单一产品的页面上插入一个样式标签。为什么不首先使用默认的wordpress style.css和一个像样的选择器呢?当我觉得事情会很复杂的时候,我喜欢它,然后我遇到了这样的答案,这很简单。奇怪的是,我有但数量仍然显示在结帐单中。很简单,更新后您将陷入混乱:)
.page-id-11111 .woocommerce .quantity .qty {
    Display:None!important;
}
// 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;
}