Php Woocommerce将自定义维度包含到$product类中
情况:Woocommerce产品对象通常包含带有原始x y z值的数组Php Woocommerce将自定义维度包含到$product类中,php,wordpress,woocommerce,product,dimension,Php,Wordpress,Woocommerce,Product,Dimension,情况:Woocommerce产品对象通常包含带有原始x y z值的数组维度 $product = [ 'dimensions' => [ 'length' => 1, 'width' => 1, 'height' => 1 ], 'dimensions_html' => '1 x 1 x 1 cm', ... 使用“”回答代码,我创建了3个新的自定义尺寸(深度、直径、阀座高度) 问题:我想将这些属性添加到product类中,
维度
$product = [
'dimensions' => [
'length' => 1,
'width' => 1,
'height' => 1
],
'dimensions_html' => '1 x 1 x 1 cm',
...
使用“”回答代码,我创建了3个新的自定义尺寸(深度、直径、阀座高度)
问题:我想将这些属性添加到product类中,以便它们在任何地方都可以直接使用,例如:
$product = [
'dimensions' => [
'length' => 1,
'width' => 1,
'height' => 1,
'depth' => 1,
'diameter' => 1,
'seat-height' => 1
],
'dimensions_html' => '1 x 1 x 1 x 1 x 1 x 1 cm',
...
如何做到这一点?我对维度进行了修改,将所有必要的维度都包含进来。这不是一个优雅或通用的解决方案,但现在对我来说很有效
// functions.php
add_filter( 'woocommerce_format_dimensions', 'change_formated_product_dimentions', 10, 2 );
function change_formated_product_dimentions( $dimension_string, $dimensions ){
global $product;
$cm = get_option( 'woocommerce_dimension_unit' );
$html = '';
if( $dimensions['length'] ){
$html .= '<span><strong>Length</strong> '.$dimensions['length'].' '.$cm.'</span>';
}
if( $dimensions['width'] ){
$html .= '<span><strong>Width</strong> '.$dimensions['width'].' '.$cm.'</span>';
}
if( $dimensions['height'] ){
$html .= '<span><strong>Height</strong> '.$dimensions['height'].' '.$cm.'</span>';
}
$depth = $product->get_meta( '_depth' );
if( $depth ){
$html .= '<span><strong>Depth</strong> '.$depth.' '.$cm.'</span>';
}
$diameter = $product->get_meta( '_diameter' );
if( $diameter ){
$html .= '<span><strong>Diameter</strong> '.$diameter.' '.$cm.'</span>';
}
$seat_height = $product->get_meta( '_seat_height' );
if( $seat ){
$html .= '<span><strong>Seat height</strong> '.$seat_height.' '.$cm.'</span>';
}
return $html;
}
//functions.php
添加过滤器('woocommerce\u format\u dimensions','change\u Formatted\u product\u dimensions',10,2);
函数更改\格式化\产品\维度($dimension\字符串,$dimensions){
全球$产品;
$cm=get_选项('woocommerce_dimension_unit');
$html='';
如果($dimensions['length'])){
$html.='Length'.$dimensions['Length'].'.$cm';
}
如果($dimensions['width'])){
$html.='Width'.$dimensions['Width'].'.$cm';
}
如果($dimensions['height'])){
$html.='高度'.$dimensions['Height'].'.$cm';
}
$depth=$product->get_meta('u depth');
如果($深度){
$html.='Depth'.$Depth.'.$cm';
}
$diameter=$product->get_meta('u diameter');
如果($直径){
$html.='直径'.$Diameter.'.$cm.';
}
$seat_height=$product->get_meta('seat_height');
若有($座){
$html.='座椅高度。$Seat_height.'.$cm';
}
返回$html;
}
这现在包含在$product['dimensions\u html']
中,并且当在
长度1厘米
宽度1厘米
高度1厘米
深度1厘米
直径1厘米
座椅高度1厘米
这(几乎)正是我想要的