在Woocommerce中更改产品库存可用性文本 [英] Change product stock availability texts in Woocommerce

查看:157
本文介绍了在Woocommerce中更改产品库存可用性文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改woocommerce中可用数量旁边的库存文本.我在产品变化中使用库存管理.

I am trying to change the in stock text next to the quantity available in 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.

感谢您的帮助.

推荐答案

以下代码将处理所有情况,包括带有自定义文本的库存量显示:

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

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce中更改产品库存可用性文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆