简体   繁体   English

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

[英]Display a custom button on WooCommerce product pages only for certain categories

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).我在连接到产品元 demo_url 的产品页面上添加了一个自定义按钮,它只是一个因每个产品而异的 URL(用于在灯箱中分别打开每个产品的实时演示)。 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.我想这应该通过 get_cat_ID( $cat_name ) 来完成,但我不确定如何编写 if 函数,而且通常我在编码方面缺乏经验。 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谢谢

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.代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

The button will appear below "add to cart" 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+所有代码都经过测试并适用于 WooCommerce 版本 3+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP中的“ If”语句。 我只想在woocommerce中显示某些产品类别的词组 - “If” statement in php. I want to display a phrase in woocommerce for only certain product categories 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages 根据 Woocommerce 中的送货区域和产品类别显示自定义消息 - Display a custom message based on shipping zone and product categories in Woocommerce 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages WooCommerce 单个产品页面上特定类别的自定义按钮 - Custom Button On WooCommerce Single Product Page For Specific Categories 在 Woocommerce 中显示链接到随机产品的自定义按钮 - Display a custom button linked to a random product in Woocommerce Woocommerce单一产品页面按钮,仅用于特定类别 - Woocommerce Single Product Page Button For Specific Categories Only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM