当产品进货时显示自定义产品徽章WooCommerce [英] Display custom product badge when a product is back in stock WooCommerce

查看:72
本文介绍了当产品进货时显示自定义产品徽章WooCommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何为新创建的产品添加新徽章。在这里,我们使用get_date_modified(),因为我们在创建产品时并不总是发布它们,并且希望它们也具有10天的新徽章。

添加新徽章的工作解决方案:

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

现在,有时一种产品会脱销。在产品进货后,我们想要展示一个像新进货一样的徽章。但使用get_date_modified()的当前解决方案时,由于get_date_modified()功能,它将始终显示新徽章。

问题: 是否有一种方法使我只能在产品从缺货到库存时才能显示徽章?

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

推荐答案

更新产品和产品库存时应添加/更新自定义产品元:

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

您可以相应地从新进货添加条件。

如何添加/更新自定义产品元:

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

您可以在每个产品中检查$EARTER_UPDATE,如果存在或为真,则可以显示从上次修改日期到当前日期的10天内的徽章

您可以相应地更改操作或条件。

这篇关于当产品进货时显示自定义产品徽章WooCommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆