Xime Paparella
Xime Paparella

Reputation: 11

Add Badge to New products

i'm trying to make a change on my Woocomerce Store. I want to add a badge to my New Products, my theme doesn't support this so i want to re-use a function that i find for Sale Products and Apply to New products but i'm not finding how to change it to make it work.

I have the next function that works for add percent to sale badge, i want just to modify for New products only adding the span with the class and text.

function custom_product_sale_flash( $output, $post, $product ) {
    global $product;
    if($product->is_on_sale()) {
        if($product->is_type( 'variable' ) )
        {
            $regular_price = $product->get_variation_regular_price();
            $sale_price = $product->get_variation_price();
        } else {
            $regular_price = $product->get_regular_price();
            $sale_price = $product->get_sale_price();
        }
        $percent_off = (($regular_price - $sale_price) / $regular_price) * 100;
        return '<span class="onsale">' . round($percent_off) . '% OFF</span>';
    }
}
add_filter( 'woocommerce_sale_flash', 'custom_product_sale_flash', 11, 3 );

Thank you in advance.

Upvotes: 1

Views: 548

Answers (1)

frederikvdbe
frederikvdbe

Reputation: 415

You can use one of the actions used in the file templates/content-product.php to hook into the rendering of the HTML.

add_action('woocommerce_before_shop_loop_item_title', function() {
    global $post;

    $days = 5;
    $offset = $days*60*60*24;

    if ( get_post_time() < date('U') - $offset )
        return '<span class="new-product">NEW</span>';
    }
});

Upvotes: 0

Related Questions