你好,我在stackoverflow上发现了这段代码,并试图将其实现到我的网站中。我遇到的问题是,一旦foreach循环完成,它返回的变量名为"3.0 - am“,而不是”3.0 am“。我已经更改了一些代码以显示一些更改,但是一旦我在其中添加了我的代码,它就不再显示缺货产品。有时,当我添加代码时,变量产品下拉列表中不会显示“脱销”文本。
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;
}我尝试的是将代码更改为:
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;
}发布于 2020-04-25 23:26:56
如果您不想对变体名称进行格式化-只需删除此行代码$term_name = str_replace(' ', '-', strtolower($term_name));
https://stackoverflow.com/questions/61415385
复制相似问题