php 仅在 woocommerce 商店/类别页面上隐藏“添加到购物车”按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26976296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:07:37  来源:igfitidea点击:

Hide 'add to cart' button ONLY on woocommerce shop/category pages

phpwordpress

提问by Seb

I want to hide the button on my shop pages, but I would like to show it on other posts and pages.

我想隐藏我的商店页面上的按钮,但我想在其他帖子和页面上显示它。

I've found this code to hide the add to cart button on my whole website:

我发现这段代码可以隐藏整个网站上的添加到购物车按钮:

add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );

function remove_add_to_cart_buttons() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}

How can I tweak it, so it only removes the button on woocommerce shop and catagory pages?

我该如何调整它,以便它只删除 woocommerce 商店和分类页面上的按钮?

回答by Kai

You can use the Woocommerce conditional tags to check: http://docs.woothemes.com/document/conditional-tags/

您可以使用 Woocommerce 条件标签来检查:http://docs.woothemes.com/document/conditional-tags/

   add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );

    function remove_add_to_cart_buttons() {
      if( is_product_category() || is_shop()) { 
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
      }
    }

回答by Stefan Radu-Tarnowetzki

This could also be done with CSS by targeting the relevant classes:

这也可以通过定位相关类使用 CSS 来完成:

.cart{display:none;}

.avia_cart_buttons{display:none;}

In my case there is that avia because i use Enfold Theme. With inspect element find out your class where the buton is located. and declare it invisible.

在我的情况下有那个avia,因为我使用Enfold Theme。使用检查元素找出按钮所在的班级。并宣布它不可见。

Another example is:

另一个例子是:

.woocommerce .products .shop-column.product-hover-style-2 .product-content 
.product-add-to-cart-btn{
    display:none !important;
}

回答by RMCS

    add_action('wp','only_add_bierkoerier_in_cart', 'woocommerce_before_cart');  

    function only_add_bierkoerier_in_cart() {

    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {


    $bierkoerier_in_cart = false;
    $categories = get_categories();

    if ( has_term( 'bierkoerier', 'product_cat', $cart_item['product_id'] ) ) {
        $bierkoerier_in_cart = true;
        break;
    } 
    }

    if($bierkoerier_in_cart) {  
        wc_print_notice( 'omdat u bierkoerier producten in uw winkelwagen heeft 
    kunt u geen winkelitems toevoegen', 'notice' );
       if(is_shop() || is_product() || is_product_category()) {
         remove_action( 'woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart' );
         remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
}
}

回答by user1457659

To remove add to cart buttons from shop, product category, and single product pages, use below steps:

要从商店、产品类别和单个产品页面中删除添加到购物车按钮,请使用以下步骤:

  1. Locate the functions.php in child theme. Child theme prevents changes been overwritten by WP updates. https://www.dreamhost.com/wordpress/create-woocommerce-child-theme/

  2. Put below code in functions.php:

  1. 在子主题中找到functions.php。子主题防止更改被 WP 更新覆盖。 https://www.dreamhost.com/wordpress/create-woocommerce-child-theme/

  2. 将下面的代码放在functions.php中:

add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 ); function remove_add_to_cart_buttons() { if( is_product_category() || is_shop()) { remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' ); } }

add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 ); function remove_add_to_cart_buttons() { if( is_product_category() || is_shop()) { remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' ); } }

add_action( 'woocommerce_single_product_summary', 'woocommerce_before_cart' ); function woocommerce_before_cart() { if( is_product()) { remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30); } }

add_action('woocommerce_single_product_summary', 'woocommerce_before_cart'); 函数 woocommerce_before_cart() { if( is_product()) { remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30); } }

回答by AAHEEL AZIZ

That's quite simple as i have gone through several tutorials when i was trying to fix it . You have to just put this code in woocommerce.php to hide add to cart button for shop page.

这很简单,因为我在尝试修复它时已经阅读了几个教程。您只需将此代码放在 woocommerce.php 中即可隐藏商店页面的添加到购物车按钮。

function WpBlog() {
  remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
  remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
  return WooCommerce::instance();
}

Hope that would work for you, if not let me know i will guide you

希望这对你有用,如果不是让我知道我会指导你

回答by Swapnali

To remove the "Add to cart"button You need to use hook which not affect other code-

要删除“添加到购物车”按钮您需要使用不影响其他代码的钩子-

add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_button', 1 );
function remove_loop_button()
{
if( is_product_category() || is_shop()) { 
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}
}

this will remove add to cart button from shop/category pages .

这将从商店/类别页面中删除添加到购物车按钮。

Here you can get WooCommerce Action and Filter Hook -https://docs.woothemes.com/wc-apidocs/hook-docs.html

在这里您可以获得 WooCommerce 操作和过滤器挂钩 - https://docs.woothemes.com/wc-apidocs/hook-docs.html

回答by Abuzer Firdousi

Here is a plugin you can use to remove hide disable add to cart button https://wordpress.org/plugins/woo-options/

这是一个插件,您可以使用它来删除隐藏禁用添加到购物车按钮https://wordpress.org/plugins/woo-options/