I know how to add a "new" badge for new created products. Here we use get_date_modified() because we not always publish products when we create them and want that they also have the "new" badge for 10 days.
Working solution for add "new" badge:
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$created = strtotime( $product->get_date_modified() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper"><img src="https://xxx.ch/wp-content/uploads/2021/01/new_badge.png"/></div>';
}
});
Now, sometimes a product is out of stock. And after a product is back in stock, we want to show a badge like "Fresh back in stock". but with the current solution of get_date_modified() it always will display the "new" badge because of the get_date_modified() function.
Question: Is there a way that I only can display a badge when a product gets from "out of stock" to "in stock"?
add_action( 'woocommerce_before_shop_loop_item_title', function() {
global $product;
$newness_days = 10;
$back_in_stock= strtotime( $product->get_date_modified() );
if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $back_in_stock && !$product->is_on_sale() && $product->is_in_stock() && get_post_meta( $product->get_id(), 'custom_badge', true) !== 'yes' ) {
echo '<div class="badge_loop_wrapper"><img src="https://xxxx.ch/wp-content/uploads/2021/01/back_in_stock_badge.png"/></div>';
}
});