在WooCommerce格式的产品尺寸输出中包括尺寸字母L / W / H [英] Include dimension letters L/W/H to WooCommerce formatted product dimensions output
问题描述
我正在尝试修改尺寸输出,该尺寸输出会在调用 get_dimensions()
的任何地方(而不是默认值)包含尺寸标注。 / p>
所以它将是1 L x 1 W x 1 H而不是1 x 1 x 1
我可以覆盖 get_dimensions()
函数,并在数组中的每个值旁边添加字母,否则我可以使用 wc_format_dimensions()
要执行此操作?
已更新:无法覆盖 get_dimensions()
WC_Product
方法。
但是您可以使用 woocommerce_format_dimensions
过滤器挂钩,该过滤器挂钩位于 wc_format_dimensions()
函数,通过这种方式(just :
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));
$ label_with_dimensions = []; //初始化
//循环虽然维数组
foreach($ dimensions as $ key => gt; $ dimention)
$ label_with_dimensions [$ key] = strtoupper(substr($键,0,1))。 ’’。 $ dimention;
返回爆破(’x’,$ label_with_dimensions)。 ’’。 get_option(‘woocommerce_dimension_unit’);
}
代码包含在您活动的子主题的functions.php文件中(
此代码已在WooCommerce 3+及更高版本上经过测试
I'm trying to modify dimensions output that everywhere where get_dimensions()
is called instead of default it would include letter of dimension.
So it would be 1 L x 1 W x 1 H instead of 1 x 1 x 1
Can I overwrite get_dimensions()
function and include letter alongside each value in array or I can somehow use wc_format_dimensions()
to do this?
Updated: You can't override get_dimensions()
WC_Product
method.
But you can use woocommerce_format_dimensions
filter hook located in wc_format_dimensions()
function, to add custom labels to each dimension product attribute, this way (just as you want):
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 ) );
$label_with_dimensions = []; // Initialising
// Loop Though dimensions array
foreach( $dimensions as $key => $dimention )
$label_with_dimensions[$key] = strtoupper( substr($key, 0, 1) ) . ' ' . $dimention;
return implode( ' x ', $label_with_dimensions) . ' ' . get_option( 'woocommerce_dimension_unit' );
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce versions 3+ and works
这篇关于在WooCommerce格式的产品尺寸输出中包括尺寸字母L / W / H的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!