php - 产品有货时显示自定义产品徽章 WooCommerce

标签 php woocommerce product

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

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

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() 函数,它始终会显示"new"徽章。

问题: 有没有办法让我只能在产品从“缺货”变为“有货”时显示徽章?

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

当产品有货 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>';
    }
});

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

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

关于php - 产品有货时显示自定义产品徽章 WooCommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69021554/

相关文章:

php - 在 WooCommerce 中的正常价格之前显示销售价格

php - 在 Woocommerce 结账时设置结账字段

php - 在 Woocommerce Recent Products 短代码中仅显示 "in stock"产品

php - 有没有什么办法可以在不安装服务器的情况下在本地测试PHP?

php - PDO 准备语句未返回预期结果

php - mySQLI - mysqli_real_escape_string 问题

php - 如何防止php项目中的 session 固定?

php - Woocommerce + Payment (PayPal) - 安全更新订单状态 + 获取交易 ID

.net - 使用 .NET 的应用程序

php - WooCommerce 产品 : Allow backorders only for specific user roles