在WooCommerce中重新排序和自定义产品尺寸格式输出 [英] Reorder and customize product dimensions formatted output in WooCommerce
问题描述
首先,我要感谢LoicTheAztec在此处给出的答案。我正在寻找非常相似的自定义,但并非100%相同,我想要实现的是: W
x L
x H
mm
First of all, I would like to thank LoicTheAztec for the answer here. I'm looking for the very similar customization, but not 100% the same, what I want to achieve is: W
xL
xH
mm
是的,最后一个计量单位。
Yes, with the unit of measurement at the end.
示例: 200x600x200mm
有了提供的答案,我设法获得了200mmx600mmx200mm
With the answer provided, I managed to get 200mmx600mmx200mm
如果我只想在末尾使用单位 mm怎么办?
What to do if I would like to have the unit "mm" at the end only?
下面是我编辑的版本代码
Below is my version of edited code
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimensions', 10, 2 );
function custom_formated_product_dimensions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );
// Set here your new array of dimensions based on existing keys/values
$new_dimensions = array(
'width' => $dimensions['width'],
'length' => $dimensions['length'],
'height' => $dimensions['height']
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );
foreach( $dimensions as $key => $dimension ){
$label_with_dimensions[$key] = $dimension . '' . get_option( 'woocommerce_dimension_unit' ). '';
}
return implode( 'x', $label_with_dimensions);
}
推荐答案
重新排序尺寸输出
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 reordered array of dimensions based on existing keys/values
$new_dimentions = array(
'width' => $dimensions['width'],
'length' => $dimensions['length'],
'height' => $dimensions['height']
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );
return implode( 'x', $dimensions) . get_option( 'woocommerce_dimension_unit' );
}
代码位于活动子主题的function.php文件中(或活动主题)。
经过测试并可以正常工作。
Tested and works.
这将输出类似 200x600x200mm
(最后只有一次 mm
)。
This will output something like 200x600x200mm
(with mm
at the end just once).
这篇关于在WooCommerce中重新排序和自定义产品尺寸格式输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!