最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce Conditional Tag inside plugin

programmeradmin3浏览0评论

I'm writing a small plugin to add some text above the 'Add To Cart' button on the WooCommerce product page.

My code:

add_action('woocommerce_before_add_to_cart_button', 'ddd_above_add_to_cart', 100);
function ddd_above_add_to_cart() { 
    if(is_product_category('vss') ) {
        echo 'hello world';
        }
}

My problem is when I use the conditional tag is_product_category('vss') to display the text based on the product category. I'm sure this error is occurring as the function is not running inside the loop, but I'm not sure how to change this.

Can anyone help me with this?

I'm writing a small plugin to add some text above the 'Add To Cart' button on the WooCommerce product page.

My code:

add_action('woocommerce_before_add_to_cart_button', 'ddd_above_add_to_cart', 100);
function ddd_above_add_to_cart() { 
    if(is_product_category('vss') ) {
        echo 'hello world';
        }
}

My problem is when I use the conditional tag is_product_category('vss') to display the text based on the product category. I'm sure this error is occurring as the function is not running inside the loop, but I'm not sure how to change this.

Can anyone help me with this?

Share Improve this question asked Dec 8, 2019 at 0:34 Nathan WilsonNathan Wilson 133 bronze badges 1
  • Hi Nathan Wilson, welcome to WordPress Development. As a rule, the community considers questions about third-party plugins (such as woocommerce) off-topic so you might notice some down-votes and answers may be slow if they come at all. That said, I have seen woocommerce dev questions get good answers so it might work out. You can find out what questions are a good fit here. – Matthew Brown aka Lord Matt Commented Dec 8, 2019 at 12:06
Add a comment  | 

1 Answer 1

Reset to default 1

Thist function not working in product page

is_product_category()

So, for me the best options is first get the terms of the current post and the next step is get the name or slug, in this case i chose the category name

$product_cat_name = $term->name;

But you can chose the slug too (better option)

$product_cat_slug = $term->slug; 

Here the documentation https://developer.wordpress/reference/functions/get_the_terms/

Example for Category name

/*Write here your own functions */
    add_action('woocommerce_before_add_to_cart_button', 'ddd_above_add_to_cart', 100);
    function ddd_above_add_to_cart() { 

            global $post;
            $terms = get_the_terms( $post->ID, 'product_cat' );
            $nterms = get_the_terms( $post->ID, 'product_tag'  );
            foreach ($terms  as $term  ) {
                $product_cat_id = $term->term_id;
                $product_cat_name = $term->name;
                break;
            }      
           //compare current category name == any category name you want
            if($product_cat_name =='vss' ) {
                echo "This Work";
            }           
    }

Example for Category Slug

/*Write here your own functions */
    add_action('woocommerce_before_add_to_cart_button', 'ddd_above_add_to_cart', 100);
    function ddd_above_add_to_cart() { 

            global $post;
            $terms = get_the_terms( $post->ID, 'product_cat' );
            $nterms = get_the_terms( $post->ID, 'product_tag'  );
            foreach ($terms  as $term  ) {
                $product_cat_id = $term->term_id;
                $product_cat_name = $term->name;
                $product_cat_slug = $term->slug;
                break;
            }      

            //compare current category slug == any category slug you want
            if($product_cat_slug =='any-slug-category' ) {
                echo "This Work";
            }           
    }
发布评论

评论列表(0)

  1. 暂无评论