【发布时间】:2018-01-06 04:46:15
【问题描述】:
我正在使用这个 woocommerce_format_dimensions 过滤器挂钩将显示的尺寸格式从 1 x 1 x 1 英寸 替换为 1 L in. x 1 W英寸 x 1 高英寸
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);
}
$dimensions 数组的 var_dump 如下所示:
array(3) { ["length"]=> string(3) "104" ["width"]=> string(3) "136" ["height"]=> string(2) "53" }
如何将 “length” 键重命名为 “diameter” 并将尺寸的顺序更改为相反的,以便最终结果为:
1 高英寸 x 1 宽英寸 x 1 深英寸
我尝试使用 array_map 重命名 $dimensions 数组中的键,但无法使其正常工作。
【问题讨论】:
标签: php wordpress woocommerce product dimensions