简体   繁体   English

在 Woocommerce 中更改产品库存可用性文本

[英]Change product stock availability texts in Woocommerce

I am trying to change the in stock text next to the quantity available in woocommerce.我正在尝试更改 woocommerce 中可用数量旁边的库存文本。 I am using the stock management in product variations.我在产品变体中使用库存管理。

I tried this code below:我在下面尝试了这段代码:

// change stock text
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $variation ) {

    // Change In Stock Text
    if (  $variation->is_in_stock() ) {
        $availability['availability'] = __('Available!', 'woocommerce');
    }

    // Change Out of Stock Text
    if ( ! $variation->is_in_stock() ) {
        echo '-------------------------';
        echo __('Sold Out', 'woocommerce');
        $availability['availability'] = __('Sold Out', 'woocommerce');
    }

    return $availability;
}

The code above changes the text but it does not pull in the stock quantity number from the variation stock manager.上面的代码更改了文本,但不会从变体库存管理器中提取库存数量。

Any help is appreciated.任何帮助表示赞赏。

The following code will handle all cases including the stock amount display with your custom texts:以下代码将处理所有情况,包括带有自定义文本的库存量显示:

add_filter( 'woocommerce_get_availability_text', 'customizing_stock_availability_text', 1, 2);
function customizing_stock_availability_text( $availability, $product ) {
    if ( ! $product->is_in_stock() ) {
        $availability = __( 'Sold Out', 'woocommerce' );
    }
    elseif ( $product->managing_stock() && $product->is_on_backorder( 1 ) )
    {
        $availability = $product->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : '';
    }
    elseif ( $product->managing_stock() )
    {
        $availability = __( 'Available!', 'woocommerce' );
        $stock_amount = $product->get_stock_quantity();

        switch ( get_option( 'woocommerce_stock_format' ) ) {
            case 'low_amount' :
                if ( $stock_amount <= get_option( 'woocommerce_notify_low_stock_amount' ) ) {
                    /* translators: %s: stock amount */
                    $availability = sprintf( __( 'Only %s Available!', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
                }
            break;
            case '' :
                /* translators: %s: stock amount */
                $availability = sprintf( __( '%s Available!', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
            break;
        }

        if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
            $availability .= ' ' . __( '(can be backordered)', 'woocommerce' );
        }
    }
    else
    {
        $availability = '';
    }

    return $availability;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce 3中检查产品库存可用性 - Checking product stock availability in Woocommerce 3 更改 WooCommerce 产品可用性 库存文本由自定义文本 - Change WooCommerce product availability In stock text by a custom text 如何根据 WooCommerce 中的不同条件更改单个产品页面上的“库存管理”可用性文本 - How to change the 'stock management' availability text on single product page based on different conditions in WooCommerce 在 Woocommerce 单个产品页面中移动库存可用性位置 - Move stock availability position in Woocommerce single product page 在Woocommerce单一产品页面中移动库存位置 - Move stock availability location in Woocommerce single product page 在 Woocommerce 存档页面中显示所有产品类型的库存可用性 - Display the stock availability for all product types in Woocommerce archive pages WooCommerce 更改尺寸和颜色的产品可用性 - WooCommerce change size and color of product availability Woocommerce产品变化:更改“库存”的位置 - Woocommerce product variations: Change position of „in stock“ 如何在 WooCommerce Product Edit Admin End 中将 In Stock 文本更改为 in stock - How to change In Stock text into in stock in WooCommerce Product Edit Admin End 在 Woocommerce 商店更改缺货产品叠加类型 - Change Out of Stock product overlay type on shop in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM