37

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

4

12 回答 12

65
  • 编辑您的产品。
  • 点击“库存”。
  • 选中“单独出售”复选框
于 2013-12-16T03:11:19.653 回答
47

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.

于 2014-06-20T02:37:37.990 回答
14

在此处输入图像描述

无需使用插件或编辑 function.php,只需检查上图中显示的选项即可。

于 2018-02-15T09:07:57.153 回答
4
.quantity, .product-quantity{display:none;}
于 2014-01-15T12:53:19.253 回答
4

我找到了一种简单的方法,只需在产品单页中进行操作,并将数量计数器保存在购物车中。只需将以下代码放入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 }
}    

您可以在此处查看 woocommerce 文档:http: //docs.woothemes.com/document/remov-product-content-based-on-category/

于 2014-12-09T12:59:57.763 回答
3

有一个免费插件可以删除可能对您有用的数量选择器。 http://wordpress.org/extend/plugins/woocommerce-remove-quantity-fields/

于 2013-02-08T18:23:48.263 回答
2

不需要插件,例如您可以使用 css 隐藏它。但是 woocommerce 只允许您销售 1 件产品,而无需选择将更多相同商品添加到购物车中。看看 woocommerce-> 设置。都在那里。

于 2013-07-31T15:59:19.000 回答
1

您还可以通过将两个数量都设置为 1 来woocommerce_quantity_input_min使用和woocommerce_quantity_input_max钩子。

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

// 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。

于 2021-03-09T19:59:09.427 回答
0

您需要编辑的模板是single-product/add-to-cart/variation-add-to-cart-button.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>
于 2016-04-06T14:48:14.220 回答
0

这个问题的其他答案没有处理一个边缘情况,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 );
于 2019-05-13T13:48:45.647 回答
0

正如此处其他答案所解释的那样,将产品(或整个商店)设置为“单独销售”现在对我有用,但我还找到了另一个解决方案:隐藏特定页面 ID 上的数量字段。如果有人出于某种原因希望该字段显示在某些页面而不是其他页面上,请考虑以下替代方法:

.page-id-11111 .woocommerce .quantity .qty {
    Display:None!important;
}

这将允许人们多次添加到购物车,并在结帐时编辑数量,同时仍然隐藏产品简码/页面上的数量字段。更多信息:从 Woocommerce 中删除数量字段,而不阻止添加多个购物车?(我已经重新设计了该站点,因此我不再需要此解决方案,但这可能对某人有所帮助。)

于 2020-05-21T13:25:23.293 回答
-4

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

在页面中找到以下代码

$availability = sprintf( __( '%s in stock', 'woocommerce' ), $this->get_total_stock() );

将此代码替换为

$availability = sprintf( __( '%s in stock', 'woocommerce' ),'');
于 2016-09-24T14:38:39.770 回答