0

I'm building a webshop with Wordpress and Woocommerce. On the productoverview (archive) of a productcategories I want to show a label if the product has the category 'New'. The following code works on the product-single, but it won't show on the productcategory-page if I add the action in content-product.php.

I used the following code to show a label on the single product page and it works as it should.

function resign_article_is_new(){

    if ( is_product() && has_term( 'Nieuw', 'product_cat' ) ) {
        echo '<p class="new-lable">New</p>';
    }
}
add_action( 'resign_new_article', 'resign_article_is_new');

When I want to show the label on the productcategory overview I used this, but the label wouldn't appear.

do_action('resign_new_article');

Hope you can tell me what I'm doing wrong

1
  • is_product() is not true on product archives. You should just remove that condition. Since it's a custom action you don't need to worry about affecting other post types. Sep 9 at 14:48
0

You need to access the Woo product loop, to output extra content on shop/archive pages (unless you use a custom template)

Try something like this:

add_action( 'woocommerce_after_shop_loop_item', 'conditional_label', 5 );
 
function conditional_label() {
   global $product;
   if (has_term( 'new', 'product_cat', $product->get_id())) {
        echo '<p class="new-lable">New</p>';
   }
}

Not tested, but should get you in the right direction.

Have a look at this hook guide, for a visualization on where you can add the text.

3
  • Unfortunately this solution seems not working. There is still nothing shown
    – Ricardio
    Sep 9 at 15:30
  • Where did you add the code? Needs to go in functions.php
    – Adnaves
    Sep 9 at 20:23
  • To keep things organised I use several functions.php (like woocommerce-functions.php etc) which I rever to in the main functions.php so I added the code in that file.
    – Ricardio
    Sep 10 at 7:59
0

After messing around with the code I found a solution to add a categorie label straight under the sales-tag on a product archive page.

//Tonen van Nieuw-label op archive page
function show_new_label(){

    //Check all the categories of taxonomy product_cat
    $argsTax = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
    $terms = wp_get_post_terms(get_the_id(), 'product_cat', $argsTax);
    
    if(count($terms) > 0){
        foreach($terms as $term){
            $output .= $term->name; //name, slug, term_id, term_group, term_taxonomy_id, taxonomy, description, parent, count
        }
    };

    //Store all taxonomies in a variable
    $outputVar = $output;

    //Check if the right taxonomy is stored in the variable
    if(strpos($outputVar, 'New') !== false){
        echo '<p class="new-lable">New</p>';
    } else{
        
    }

}
//Attach the function to the 'woocommerce_before_shop_loop_item'-loop
add_action( 'woocommerce_before_shop_loop_item', 'show_new_label', 10 );

Maybe not the most clean code, but it works perfect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.