dongzhijing8202 2017-07-15 13:34
浏览 115

仅针对特定类别在WooCommerce产品页面上显示自定义按钮

I have added a custom button to product page that is connected to product meta demo_url, which is just an URL that changes by each product (to open live demo for each product separately in a lightbox). The code is quite simple:

function my_extra_button_on_product_page() {
  global $product;
  $demo_url = get_post_meta( get_the_ID(), 'demo_url', true );
  echo '<a class="fancybox iframe" data-width="1280" data-height="720" href="'.$demo_url.'"><button style="background: lightblue; padding-left: 19px; padding-right: 19px;">Przymierz</button></a>';
}

The thing is, there are some other products in the shop that do not use live demo function. They are listed in different categories. I would like this button to be visible ONLY for certain product category (or invisible for two product categories - whatever is easier).

I suppose this should be done via get_cat_ID( $cat_name ), but I am unsure how to write the if function and generally I'm quite inexperienced in coding. Maybe something like this could work?

if get_cat_ID( $cat_name ) = categoryname {
    echo 'button code';
else
    echo 'no button code';
endif
}

How can I make it work?

Thanks

  • 写回答

1条回答 默认 最新

  • doukang8949 2017-07-15 14:29
    关注

    You can achieve it with the hooked function below, defining in it all your needed product categories:

    add_action( 'woocommerce_single_product_summary', 'custom_button_by_categories', 32 ,0 );
    function custom_button_by_categories(){
    
        global $product;
    
        // Define your categories in this array (can be Ids, slugs or names)
        $product_cats = array('clothing', 'music', 'furnitures');
    
        if( has_term( $product_cats, 'product_cat', $product->get_id() ) ){
             $demo_url = get_post_meta( $product->get_id(), 'demo_url', true );
             echo '<a class="fancybox iframe" data-width="1280" data-height="720" href="'.$demo_url.'"><button style="background: lightblue; padding-left: 19px; padding-right: 19px;">Przymierz</button></a>';
        }
    }
    

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

    The button will appear bellow addtocart button for your defined categories. You don't need to customize any template.

    If you want to remove add-to-cart button on that categories (to keep your custom button only, you can add in the condition this line:

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30, 0);
    

    All code is tested and works in WooCommerce version 3+

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题