我正在使用这个 woocommerce_format_dimensions 过滤器钩子将显示的尺寸格式从 1 x 1 x 1 in 替换为 1 L英寸 x 1 宽英寸 x 1 高英寸
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);
}
$dimensions 数组的
var_dump 如下所示:
var_dump of $dimensions array looks like this:
array(3) { ["length"]=> string(3) "104" ["width"]=> string(3) "136" ["height"]=> string(2) "53" }
我如何将 length" 键重命名为 diameter" 并将尺寸顺序更改为相反,以便最终结果是:
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 高英寸 x 1 宽英寸 x 1 深英寸
我尝试使用 array_map 重命名 $dimensions 数组中的键,但无法使其正常工作.
I have tried to rename keys in $dimensions array using array_map, but couldn't manage to get it working.
2020 年更新
您只需要设置array keys/values 如您希望在函数中使用它们(重命名一个键并重新排序数组),这样:
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 格式的产品尺寸输出中将长度重命名为直径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽