在 WooCommerce 可变产品选择器中更改库存状态名称?

Change stock status names in WooCommerce variable products selector?

我有这个代码:

$stock_status = $variation_obj->get_stock_status();
$stock_qty = $variation_obj->get_stock_quantity();
if( $stock_qty>0 )
    return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
else
    return $term_name .= ' - ' . $stock_status;

}

我用它来显示变体库存状态,例如 "In Stock" 或 "Out of Stock"。目前我的这部分功能有效,但显示库存状态是这样的:

所以我想问问别人如何显示 "In Stock" 而不是 "instock" ,如下图所示。
我尝试使用一些功能来自定义库存状态,但看起来没有任何效果。

有什么帮助吗?

谢谢。

你可以简单地使用 str_replace() PHP 函数来做到这一点:

$stock_status = $variation_obj->get_stock_status();

// Here we change both $stock_status names to human readable strings:
$stock_status = str_replace( array('instock','outofstock'), array('In Stock','Out of Stock'), $stock_status );

$stock_qty = $variation_obj->get_stock_quantity();

if( $stock_qty>0 )
    return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
else
    return $term_name .= ' - ' . $stock_status;

}