NEW and BACK IN STORE badge display - WooCommerce product

Viewed 274

Based on that question Display custom product badge when a product is back in stock WooCommerce I created a snipped to display NEW or BACK IN STORE badge based on conditions. We want, if the product is published the first time to display the NEW badge for 15 days. IF a product is out of stock in the future and comes available again, then we want to display BACK IN STOCK badge. By default when we only use the modified date, then it will always display the NEW badge.

That's why we created this code below but it does not fully work :(

The NEW badge is displayed right for the given time. We have problems to display the BACK IN STORE badge.

Is there a logical error in my code or what i'm doing wrong?

// Check if product was out of stock
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);     
   }
}

add_action('woocommerce_checkout_order_processed', 'stocktest');

function stocktest($order_id){
    $order = wc_get_order( $order_id );
    $order_item = $order->get_items();

    foreach( $order_item as $product ) {
        
    $productID = $product['product_id'];
    //for the topic I programmed the IDs hardcoded 
        if($productID){
            $quantity = $product['quantity']; 
            global $product;
            $connected_product = wc_get_product($productID);
            $connected_qty = $connected_product->get_stock_quantity();
           if(isset($connected_qty)){
            $qty = $connected_qty - $quantity;
           if ($qty < 1) {
            update_post_meta( $productID, '_outofstockproduct',  true);
            }
          }
        }
    }
}


// Set "NEW" badge for new products (first time published) 
function action_woocommerce_before_shop_loop_item_title() {
    global $product;
    
    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // 15 days in seconds
        $days_count = 15 * 24 * 60 *60; //15 * 24 * 60 *60  

        // Get now datetime (from Woocommerce datetime object) + Get now timestamp
        $datetime_now = new WC_DateTime();
        $timestamp_now = $datetime_now->getTimestamp();
        
        // Get product created datetime + product created timestamp
        $datetime_created  = $product->get_date_created();

        $timestamp_created = $datetime_created->getTimestamp();
        // print_r($datetime_created);
        // exit();
        
        // Difference in seconds between now and date created
        $time_delta_created = $timestamp_now - $timestamp_created;
        
        // If the difference is less than 10, apply "New" label
        if ( $time_delta_created < $days_count && $product->is_in_stock() ) {
            
            echo '<div class="badge_loop_wrapper"><img src="/wp-content/uploads/2021/01/new_badge.png"/></div>';
            // echo '<span>' . __( 'New Label', 'woocommerce' ) . '</span>';
        } 
        
        // Set "BACK IN STOCK" badge for products which are not published the first time and was out of stock
        else {

            global $product;
            $newness_days  = 15;
            $product_id    = $product->get_id();
            $date_modified = $product->get_date_modified()->date('Y-m-d');
            
            // Add 15 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 );
            $outofstockproduct = get_post_meta( $product_id, '_outofstockproduct', true );


            if (($outofstockproduct && $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">'.__('Back in stock').'</div>';
                echo '<div class="badge_loop_wrapper"><img src="/wp-content/uploads/2021/09/back_in_stock_badge.png"/></div>';
            }
        }
    }
}
add_action( 'woocommerce_before_shop_loop_item_title', 'action_woocommerce_before_shop_loop_item_title', 10 );

1 Answers

You should add/update product meta data when update product:

Add/update custom product meta:

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

function back_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">'.__('Back in stock').'</div>';
    }
});

You can change the action and condition accordingly. Let me know if any other question. Thanks!

Related