dongzheng4556
2017-08-17 11:06
浏览 90
已采纳

在WooCommerce中输出特定产品类别的自定义短代码

I am trying to add an extra button to my products on my shop page but only to a specific category. I have gotten this far but I cannot seem to call it up right.

if (!function_exists('add_accessory_button')){
function add_accessory_button() {
  global $product;
  $product_cat = $product->product_cat;

  if( has_term( 'dealqualify',$product_cat ) && in_the_loop() ) {
    $link = get_post_meta( $product->ID, ‘acc_link’, true );
    echo do_shortcode('<br>[button link="' . esc_attr($link) . '"]View Accessory[/button]');
} }
add_action('woocommerce_after_shop_loop_item','add_accessory_button');
}

Any help to pinpoint my mistake would be appreciated.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在我的商店页面上为我的产品添加一个额外的按钮,但仅限于特定的类别。 我已经做到这一点了,但我似乎无法正确调用它。

  if(!function_exists('add_accessory_button')){
function add_accessory_button(){
 global $  product; 
 $ product_cat = $ product-&gt; product_cat; 
 
 if(has_term('dealqualify',$ product_cat)&amp;&amp; in_the_loop()){
 $ link = get_post_meta($ product-&gt;  ID,'acc_link',true); 
 echo do_shortcode('&lt; br&gt; [button link =“'。esc_attr($ link)。'”]查看附件[/ button]'); 
}} 
add_action  ('woocommerce_after_shop_loop_item','add_accessory_button'); 
} 
   
 
 

任何帮助查明我的错误都将不胜感激。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzaotiao2863 2017-08-17 11:40
    已采纳

    There is a lot of errors in your code

    You need to set has_term() this way in your condition to make it work:

    has_term( 'dealqualify', 'product_cat', $product->get_id() )
    

    Also for the product ID do it this way: $product->get_id()... I have try to correct the others errors too...

    So your code should be something like:

    if ( ! function_exists('add_accessory_button')){
    
        function add_accessory_button() {
            global $product;
            if( has_term( 'dealqualify', 'product_cat', $product->get_id() ) && in_the_loop() ) {
                $link = get_post_meta( $product->get_id(), 'acc_link', true );
                echo '<br>' . do_shortcode("[button link='$link']View Accessory[/button]");
        }
        add_action('woocommerce_after_shop_loop_item','add_accessory_button');
    }
    

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

    This should work now…

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题