如果 WooCommerce 中未启用 "Manage Stock",则显示自定义股票消息

Display custom stock message if "Manage Stock" is not enabled in WooCommerce

如果未选中在产品级别启用库存管理,我想在产品页面中显示有关库存状态的消息

当库存状态为

可以吗?

使用以下代码,您可以检查库存状态并相应地调整消息。

function change_stock_message( $text, $product ) {  
    // Managing stock NOT checked
    if ( !$product->managing_stock() ) {

        // Get stock status
        if ( method_exists( $product, 'get_stock_status' ) ) {
            $stock_status = $product->get_stock_status();
        }

        // Check stock status, adjust the message accordingly
        switch ( $stock_status ) {
            case 'instock':
                $text = __( 'Available', 'woocommerce' );
                break;
            case 'outofstock':
                $text = __( 'Out of stock', 'woocommerce' );
                break;
            case 'onbackorder':
                $text = __( 'Available after 4-7 days', 'woocommerce' );
                break;
        }
    }

    return $text;
}
add_filter( 'woocommerce_get_availability_text', 'change_stock_message', 10, 2 );