在WooCommerce格式化的产品尺寸输出中将长度重命名为直径 [英] Rename Length to Diameter in WooCommerce formatted product dimensions output
问题描述
我正在使用此 woocommerce_format_dimensions
过滤器挂钩替换 1 x 1 x 1 in 中显示的尺寸格式到 1 L in。x 1 W in。x 1 H in。
I’m using this woocommerce_format_dimensions
filter hook to replace displayed dimensions format from 1 x 1 x 1 in to 1 L in. x 1 W in. x 1 H in.
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
foreach( $dimensions as $key => $dimention )
$label_with_dimensions[$key] = $dimention . ' ' . strtoupper( substr($key, 0, 1) ) . ' ' . get_option( 'woocommerce_dimension_unit' ) . '.';
return implode( ' x ', $label_with_dimensions);
}
var_dump
$ dimensions 数组中的> 看起来像这样:
var_dump
of $dimensions array looks like this:
array(3) { ["length"]=> string(3) "104" ["width"]=> string(3) "136" ["height"]=> string(2) "53" }
如何重命名长度 键入直径 并将尺寸顺序更改为相反的顺序,这样最终结果将是:
How could I rename "length" key to "diameter" and change the order of dimensions to be in reverse, so that final result would be:
1 H in。x 1 W in。x 1 D in。
我尝试使用以下命令重命名 $ dimensions 数组中的键 array_map
,但无法使其正常工作。
I have tried to rename keys in $dimensions array using array_map
, but couldn't manage to get it working.
推荐答案
更新2020
您只需要设置 array
键
/ 值
它们在您的函数中(重新命名一个键并重新排列数组),方法是:
You just need to set the array
keys
/values
as you want them in your function (renaming one key and reordering your array), this way:
add_filter( 'woocommerce_format_dimensions', 'Custom_formated_product_dimentions_with_labels', 10, 2 );
function Custom_formated_product_dimentions_with_labels( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
// Set here your new array of dimensions based on existing keys/values
$new_dimentions = array(
'height' => $dimensions['height'],
'width' => $dimensions['width'],
'diameter' => $dimensions['length']
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );
$label_with_dimensions = array();
foreach( $dimensions as $key => $dimention ){
$dimensions[$key] = ucfirst($key) . ' ' . $dimention . ' ' . get_option( 'woocommerce_dimension_unit' );
}
return implode( ' x ', $dimensions) . '.';
}
代码位于您的活动子主题(或主题)的function.php文件中,或者
此代码已在WooCommerce版本3+上进行了测试,并且可以正常工作
This code is tested on WooCommerce versions 3+ and works
这篇关于在WooCommerce格式化的产品尺寸输出中将长度重命名为直径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!