首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改woocommerce变量产品下拉列表的输出文本

更改woocommerce变量产品下拉列表的输出文本
EN

Stack Overflow用户
提问于 2020-04-25 02:46:53
回答 1查看 75关注 0票数 0

你好,我在stackoverflow上发现了这段代码,并试图将其实现到我的网站中。我遇到的问题是,一旦foreach循环完成,它返回的变量名为"3.0 - am“,而不是”3.0 am“。我已经更改了一些代码以显示一些更改,但是一旦我在其中添加了我的代码,它就不再显示缺货产品。有时,当我添加代码时,变量产品下拉列表中不会显示“脱销”文本。

代码语言:javascript
复制
add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 999, 1 );

function customizing_variations_terms_name( $term_name ) {

global $product;
// Get available product variations
$product_variations = $product->get_available_variations();

// product_variation terms are normalized to lowercase with
// spaces replaced by dashes.
// if your term name contains capital letters or spaces i.e. 'SIZE 6'
// the product variation will be lowercase with dashes, i.e. 'size-6'

     $term_name = str_replace(' ', '-', strtolower($term_name));


foreach ( $product_variations as $product_variation ) {
    if( isset( $product_variation['attributes'] ) ) {
        $key = array_search($term_name, $product_variation['attributes']);

        if( $key !== false && ! $product_variation['is_in_stock'] ) {
            return $term_name . ' - Out of Stock';
        }
    }
}

return $term_name;
}

我尝试的是将代码更改为:

代码语言:javascript
复制
foreach ( $product_variations as $product_variation ) {
    if( isset( $product_variation['attributes'] ) ) {
        $key = array_search($term_name, $product_variation['attributes']);

        if( $key !== false && ! $product_variation['is_in_stock'] ) {
            **$term_name = str_replace('-', ' ', strtolower($term_name));**
            return $term_name . ' - Out of Stock';
        }
    }
}
**$term_name = str_replace('-', ' ', strtolower($term_name));**
return $term_name;
}
EN

回答 1

Stack Overflow用户

发布于 2020-04-25 23:26:56

如果您不想对变体名称进行格式化-只需删除此行代码$term_name = str_replace(' ', '-', strtolower($term_name));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61415385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档