隐藏某些类别的"添加到购物车"



我需要从任何地方(从所有页面(删除添加到购物车按钮以及特定类别产品(例如法律服务(的数量输入字段。在这样做的过程中,将那些添加其他插件的元素留在添加到购物车按钮所在的区域中。

我会使用这个代码:

// Disable products from categories (a, b, c) from being purchased no matter what
add_filter('woocommerce_is_purchasable', 'set_catalog_mode_on_for_category', 10, 2 );

function set_catalog_mode_on_for_category( $is_purchasable, $product ) {

if( has_term( array( 'a', 'b', 'c' ), 'product_cat', $product->get_id() ) ) {
return false;
}

return $is_purchasable;
}
// Hide 'Add To Cart' for variable products, but keep product variations visible
function remove_add_to_cart(){
if( has_term( array( 'a', 'b', 'c' ), 'product_cat', $product->get_id() ) ) {

remove_action( 'woocommerce_single_variation','woocommerce_single_variation_add_to_cart_button', 20 );
}
}

但随后其他插件添加到"添加到购物车"区域的其他按钮会随着"添加到车"按钮一起消失。

然后我想也许我可以使用这个代码:

add_action( 'woocommerce_single_product_summary', 'wp_replace_add_to_cart_btn', 31 );
function wp_replace_add_to_cart_btn() {
if( has_term( array( 'a', 'b', 'c' ), 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');

?>
<style>div.quantity,.single_add_to_cart_button{display:none}</style>
<style>div.quantity,.qty{display:none}</style>
<script>jQuery(document).ready(function($){$('div.quantity,.single_add_to_cart_button'),remove()})</script>
<script>jQuery(document).ready(function($){$('div.quantity,.qty'),remove()})</script>
<?php
}
}

但在这种情况下,购物车按钮仅在产品页面上消失,而保留在存档产品页面(商店页面(上。

这种方法的另一个缺点是,它不适用于其他类别的产品显示在"相关产品"小部件中的页面,在该小部件中允许添加到购物车按钮。

你能帮助完善最后的代码或提供更好的解决方案吗。

我不想使用将商店变成目录的插件,因为它们有太多的代码,然后页面加载需要1-2秒的时间。请提供正确的解决方案。

事实证明,这个解决方案可以处理错误。我将把它作为错误的解决方案留在这里,下面我将发布一个带有正确解决方案的新答案。

add_action( 'woocommerce_after_shop_loop_item', 'wp_replace_add_to_cart_btn', 31 );
function wp_replace_add_to_cart_btn() {

$terms = get_the_terms( get_the_ID(), 'product_cat' );
foreach ( $terms as $term ) {
if (has_term( array( 'a', 'b', 'c' ), 'product_cat', get_the_id ())) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');

?>
<style>div.quantity,.single_add_to_cart_button{display:none}</style>
<style>div.quantity,.qty{display:none}</style>
<script>jQuery(document).ready(function($){$('div.quantity,.single_add_to_cart_button'),remove()})</script>
<script>jQuery(document).ready(function($){$('div.quantity,.qty'),remove()})</script>
<?php
}

else{
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}

}
}

这是该解决方案的改进代码https://stackoverflow.com/a/70434889/16275280

此代码执行以下操作:从特定类别的单个项目页面中删除"添加到购物车"one_answers"数量"按钮。在商店页面、存档产品页面和相关产品小部件中将循环"添加到购物车"按钮重命名为"查看产品"。

也许我在这里唯一错过的是更改按钮的工具提示文本。不幸的是,我还没有想好办法。

/**
* Replace 'Add to cart' button to link on Shop Page and Archive Pages if is Simple Product
*/
function my_replacing_add_to_cart_button( $button, $product  ) {
$categories = array( 'a', 'r', 'w' );

if (has_term( $categories, 'product_cat', get_the_id() ) ) {

if ( $product->is_type( 'simple' ) ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
}
return $button;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_replacing_add_to_cart_button', 10, 2 );
/**
* Remove Add to Cart button from product description of product if is product category (for all product types)
*/
function my_replace_add_to_cart_btn() {
global $product;

$categories = array( 'a', 'r', 'w' );
if (has_term( $categories, 'product_cat', get_the_id() ) ) {

?>
//Remove 'Add_to_cart' button and 'Quantity' input box with CSS 
<style>div.quantity,.qty{display:none}</style>
<style>div.quantity,.single_add_to_cart_button{display:none}</style>

// Remove 'Add_to_cart' button and 'Quantity' input box, restrict user use inspect element in browser
<script>jQuery(document).ready(function($){$('div.quantity,.qty').remove();})</script>
<script>jQuery(document).ready(function($){$('div.quantity,.single_add_to_cart_button').remove();})</script>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'my_replace_add_to_cart_btn', 31 );

最新更新