我需要为定义的产品类别的产品隐藏添加到购物车"按钮.
我希望确定的数量仍然可见,因为我正在使用 Yith 请求报价插件,该插件使用报价系统的数量.
目标:为保留数量字段的特定产品类别隐藏添加到购物车"按钮.
我正在寻找一小段代码放在我的 functions.php
文件中.
更新:(在简单产品中添加了 WooCommerce 产品插件插件的兼容性).
这里是(对于定义的简单和可变产品类型的产品类别)的方法:
代码:
//无按钮加回数量功能(可变产品)函数 add_back_quantities_variable_products(){全球$产品;?><div class="woocommerce-variation-add-to-cart changes_button"><?phpdo_action('woocommerce_before_add_to_cart_quantity');woocommerce_quantity_input(数组('min_value' =>apply_filters('woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),'max_value' =>apply_filters('woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product),'输入值' =>isset( $_POST['quantity'] ) ?wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),) );do_action('woocommerce_after_add_to_cart_quantity');?><input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>"/><input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>"/><input type="hidden" name="variation_id" class="variation_id" value="0"/>
<?php}//没有按钮的功能加回数量(简单产品)函数 add_back_quantities_simple_products(){全球$产品;如果(!$product->is_purchasable())返回;回声 wc_get_stock_html( $product );如果 ( $product->is_in_stock() ) : ?><?php do_action('woocommerce_before_add_to_cart_form');?><form class="cart" method="post" enctype='multipart/form-data'><?php//对于 WooCommerce 产品附加组件(更新)do_action('woocommerce_before_add_to_cart_button');do_action('woocommerce_before_add_to_cart_quantity');woocommerce_quantity_input(数组('min_value' =>apply_filters('woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),'max_value' =>apply_filters('woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product),'输入值' =>isset( $_POST['quantity'] ) ?wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),) );do_action('woocommerce_after_add_to_cart_quantity');?></表单><?phpdo_action('woocommerce_after_add_to_cart_form');万一;}//在单个产品页面中用您的自定义按钮替换添加到购物车按钮和数量add_action('woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0);函数conditionally_replacing_template_single_add_to_cart() {全球 $product, $post;//在此处设置数组中的产品类别$terms = array('T恤','手套');if( has_term( $terms, 'product_cat' ) ){//对于可变产品类型if($product->is_type('变量')){//删除添加到购物车按钮和数量remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);//添加不带按钮的数量add_action('woocommerce_single_variation', 'add_back_quantities_variable_products', 20);}//对于简单的产品类型否则 if( $product->is_type( 'simple' ) ){//删除添加到购物车按钮和数量remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);//添加不带按钮的数量add_action('woocommerce_single_product_summary', 'add_back_quantities_simple_products', 30);}}}
也可选择(对于档案页面):
//用商店和档案页面中的产品链接代替添加到购物车的按钮//对于可变和简单的产品add_filter('woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2);函数 replace_loop_add_to_cart_button( $button, $product ) {//在此处设置数组中的产品类别$terms = array('T恤','手套');//仅适用于简单产品if(!$product->is_type('variable')) 返回;if( has_term( $terms, 'product_cat', $product->get_id() ) ){$button_text = __( "查看产品", "woocommerce");$button = '<a class="button" href="' . $product->get_permalink() .'">'.$button_text .'</a>';}返回 $ 按钮;}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
此代码已在 WooCommerce 3+ 下测试并有效.
<小时>对于产品标签 - 如果您希望它与产品标签一起使用,您将替换:
if( has_term( $terms, 'product_cat' ) ){
由
if( has_term( $terms, 'product_tag' ) ){
I need to hide the "add to cart" button for products of a defined product category.
I would like the quantity fied still visible because I am using the Yith request a quote plugin which uses quantities for the quote system.
The goal: hide "add to cart" button for a certain product category keeping the quantities fields.
Im looking for a short string of code to place in my functions.php
file.
Updated: (Added compatibility for WooCommerce Product Add-ons plugin in simple products).
Here is the way (for defined product categories for simple and variable product types) to:
The code:
// function add back quantities without button (variable product)
function add_back_quantities_variable_products(){
global $product;
?>
<div class="woocommerce-variation-add-to-cart variations_button">
<?php
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
<input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
<?php
}
// function add back quantities without button (simple product)
function add_back_quantities_simple_products(){
global $product;
if ( ! $product->is_purchasable() ) return;
echo wc_get_stock_html( $product );
if ( $product->is_in_stock() ) : ?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php
// For WooCommerce Product add-ons (Update)
do_action( 'woocommerce_before_add_to_cart_button' );
do_action( 'woocommerce_before_add_to_cart_quantity' );
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
) );
do_action( 'woocommerce_after_add_to_cart_quantity' );
?>
</form>
<?php
do_action( 'woocommerce_after_add_to_cart_form' );
endif;
}
// Replacing add to cart button and quantities by your custom button in Single product pages
add_action( 'woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0 );
function conditionally_replacing_template_single_add_to_cart() {
global $product, $post;
// Set HERE your product categories in the array
$terms = array( 't-shirts', 'gloves' );
if( has_term( $terms, 'product_cat' ) ){
// For variable product types
if( $product->is_type( 'variable' ) ){
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
// Add back quantities without button
add_action( 'woocommerce_single_variation', 'add_back_quantities_variable_products', 20 );
}
// For simple product types
else if( $product->is_type( 'simple' ) )
{
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// Add back quantities without button
add_action( 'woocommerce_single_product_summary', 'add_back_quantities_simple_products', 30 );
}
}
}
And optionally (for archives pages):
// Replacing the button add to cart by a link to the product in Shop and archives pages
// For variable and simple products
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Set HERE your product categories in the array
$terms = array( 't-shirts', 'gloves' );
// Only for simple products
if( ! $product->is_type( 'variable' ) ) return;
if( has_term( $terms, 'product_cat', $product->get_id() ) ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested under WooCommerce 3+ and works.
For Product tags - If you want that to work with product tags, you will replace:
if( has_term( $terms, 'product_cat' ) ){
by
if( has_term( $terms, 'product_tag' ) ){
这篇关于删除 WooCommerce 3 中特定产品类别的添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!