Nik7
Nik7

Reputation: 386

Display custom product badge when a product is back in stock WooCommerce

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>';
   }
});

Upvotes: 2

Views: 1003

Answers (1)

Rajeev Singh
Rajeev Singh

Reputation: 1809

You should add/update custom product meta when update product and product stock like this:

if ($stock_qty >=1) { 
    update_post_meta( $product_id, '_earlier_update',  true);
} 
else{
    update_post_meta( $product_id, '_earlier_update',  false);
}

You can add conditions from Fresh back in stock accordingly.

How to add/update custom product meta:

add_action( 'woocommerce_updated_product_stock', 'woo_updated_product_stock', 10, 3);
add_action( 'woocommerce_update_product', 'woo_updated_product_stock',10, 3 );
add_action('save_post_product', 'woo_updated_product_stock', 10, 3);

function woo_updated_product_stock( $product_id ) {

   $product  = wc_get_product( $product_id );

   $stock_qty  = $product->get_stock_quantity();

   if ($stock_qty >=1) { 
      update_post_meta( $product_id, '_earlier_update',  true);
   } 
   else{
      update_post_meta( $product_id, '_earlier_update',  false);
   }
}

Display custom product badge when a product is back in stock WooCommerce

add_action( 'woocommerce_before_shop_loop_item_title', function() {      
    global $product;
    $newness_days  = 10;
    $product_id    = $product->get_id();
    $date_modified = $product->get_date_modified()->date('Y-m-d');
    // Add 10 day on product modify date  becase fresh back in stock will show 10 days from current modify dates
    $back_in_stock = strtotime($date_modified. " + $newness_days day");
    $today         = strtotime(date('Y-m-d'));

    $earlier_update = get_post_meta( $product_id, '_earlier_update', true );

    if (($earlier_update && $today < $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">'.__('Fresh back in stock').'</div>';
    }
});

Where you can check $earlier_update in every product and if exist or is true, then you can show the badge 10 days from the last modified date to the current date

You can change action or condition accordingly.

Upvotes: 2

Related Questions