dongrong5189 2016-12-16 12:11
浏览 100

隐藏添加到特定类别的购物车

I have tried many code examples and none of them have worked.

My site has 2 main categories (New & Used) each have about 10 sub cats with them. I am using the YITH request a quote plugin but I only want to use it on the Used items, Is there a way I can display the Add To Cart button for just the New category?

the url is set up like site.com/used-equipment/sub-category/product-name/

Here is the code I have tried.

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

    function remove_add_to_cart_buttons() {
      if( is_product_category( array( 'used-equipment' )) { 
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
      }
    }
  • 写回答

1条回答 默认 最新

  • dqc3469 2016-12-16 13:43
    关注

    — Updated —

    You need to use has_term() conditional function to make it work. has_term() accept category or subcategory names, slugs or Ids, (a single string or an array of values).

    Here is your changed code:

    add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
    
    function remove_add_to_cart_buttons() {
    
        if( has_term( 'used-equipment', 'product_cat')) {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        }
    }
    

    This code is tested and works. It removes on the shop pages and on archives pages the add-to-cart button on products that belongs to a defined category or subcategory…

    This goes in function.php file of your active child theme (or theme) or also in any plugin file.

    评论

报告相同问题?

悬赏问题

  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏