从1×1×1中重命名长度与直径在WooCommerce格式化产品的尺寸输出
问题描述:
我使用这个woocommerce_format_dimensions
滤波器钩来替代显示尺寸格式1升。×1瓦。X 1的H从<strong>1×1×1中</strong>重命名长度与直径在WooCommerce格式化产品的尺寸输出
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
的$尺寸阵列看起来是这样的:
array(3) { ["length"]=> string(3) "104" ["width"]=> string(3) "136" ["height"]=> string(2) "53" }
我怎样才能重新命名“长度”键“直径”和改变尺寸的顺序以反色,所以最后的结果将是:在
1高x 1瓦。×1 d。
我试图重新命名阵列使用array_map
在$尺寸按键,但不能设法得到它的工作。
答
你只需要设置array
keys
/values
,你希望他们在你的函数(重命名一个键,并重新排序的数组),这样一来:
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');
// 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));
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);
}
代码去在你的活动子主题(或主题)的function.php文件中,或者也在任何插件文件中。
此代码在WooCommerce版本3+上测试并且工作