我需要在产品详细信息页面上的 Woocommerce 中隐藏“数量”字段(在添加到购物车之前输入数量),并且只显示“添加到购物车”按钮,然后将数量 1在购物车中。原因是因为我根据重力形式收集数量。
12 回答
- 编辑您的产品。
- 点击“库存”。
- 选中“单独出售”复选框
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.
.quantity, .product-quantity{display:none;}
我找到了一种简单的方法,只需在产品单页中进行操作,并将数量计数器保存在购物车中。只需将以下代码放入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/
有一个免费插件可以删除可能对您有用的数量选择器。 http://wordpress.org/extend/plugins/woocommerce-remove-quantity-fields/
不需要插件,例如您可以使用 css 隐藏它。但是 woocommerce 只允许您销售 1 件产品,而无需选择将更多相同商品添加到购物车中。看看 woocommerce-> 设置。都在那里。
您还可以通过将两个数量都设置为 1 来woocommerce_quantity_input_min
使用和woocommerce_quantity_input_max
钩子。
实际上,在模板中,如果min和max具有相同的值/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。
您需要编辑的模板是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>
这个问题的其他答案没有处理一个边缘情况,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 );
正如此处其他答案所解释的那样,将产品(或整个商店)设置为“单独销售”现在对我有用,但我还找到了另一个解决方案:隐藏特定页面 ID 上的数量字段。如果有人出于某种原因希望该字段显示在某些页面而不是其他页面上,请考虑以下替代方法:
.page-id-11111 .woocommerce .quantity .qty {
Display:None!important;
}
这将允许人们多次添加到购物车,并在结帐时编辑数量,同时仍然隐藏产品简码/页面上的数量字段。更多信息:从 Woocommerce 中删除数量字段,而不阻止添加多个购物车?(我已经重新设计了该站点,因此我不再需要此解决方案,但这可能对某人有所帮助。)
很简单,在 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' ),'');