WooCommerce 自定义缺货文本 + 特定产品 ID

WooCommerce custom out of stock text + specific product id

这是我正在尝试做的事情

  1. 检测特定产品是否有货
  2. 如果是,请编辑自定义库存消息
  3. 在 A2C 按钮上方显示自定义消息(库存数量通知所在)

问题:目前正在编辑所有产品。我没有成功地将它应用到特定的产品 ID。

这是我目前尝试的方法

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
    global $product;

    // custom 
    if ( $_product->is_in_stock() && $product_id = '6498' ) {
        $availability['availability'] = sprintf( __('✔️ Available but low in stock | 30-day No Questions Asked Money-Back Guarantee Applies', 'woocommerce'), $product->get_stock_quantity());
    }

    // Out of stock
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
    }

    return $availability;

}

谢谢!

试试这个代码

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 10, 2);
function wcs_custom_get_availability( $availability, $_product ) { 
    // custom 
    if ( $_product->is_in_stock() && $_product->get_id() == '6498' ) {
        $availability['availability'] = sprintf( __('✔️ Available but low in stock | 30-day No Questions Asked Money-Back Guarantee Applies', 'woocommerce'), $_product->get_stock_quantity());
    }

    // Out of stock
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Sorry, All sold out!', 'woocommerce');
    }

    return $availability;
}