1 回答
TA贡献1789条经验 获得超10个赞
首先,该方法get_dimensions()有一个显示格式化尺寸的选项,因此您可以使用以下命令更改尺寸格式,以根据需要添加尺寸标签:
add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
$text_domain = 'woocommerce';
$dimension_unit = get_option('woocommerce_dimensions_unit');
if ( empty( $dimension_string ) )
return __( 'N/A', $text_domain );
// Set here your new array of dimensions based on existing keys/values
$new_dimensions = array(
__('width', $text_domain) => $dimensions['width'],
__('length', $text_domain) => $dimensions['length'],
__('height', $text_domain) => $dimensions['height'],
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );
// Loop through new dimensions array adding spanish labels
foreach( $dimensions as $key => $dimention ){
$dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . $dimension_unit;
}
return implode( ' × ', $dimensions);
}
对于西班牙语,您可以使用Loco 插件翻译字符串,也可以替换:
$new_dimensions = array(
__('width', $text_domain) => $dimensions['width'],
__('length', $text_domain) => $dimensions['length'],
__('height', $text_domain) => $dimensions['height'],
);
经过:
$new_dimensions = array(
__('longitud', $text_domain) => $dimensions['width'],
__('ancho', $text_domain) => $dimensions['length'],
__('alto', $text_domain) => $dimensions['height'],
);
现在,在您的代码中,您将使用get_dimensions( true )方法来显示格式化尺寸,如下所示:
// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
$product = $cart_item['data'];
if ( $product->has_dimensions() ){
$cart_item_data[] = array(
'name' => __( 'Dimensions', 'woocommerce' ),
'value' => $product->get_dimensions( true ),
);
}
return $cart_item_data;
}
// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 10, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
$product = $values['data'];
if ( $product->has_dimensions() ){
$item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $product->get_dimensions( true ) );
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
注意:由于has_dimensions()条件方法返回布尔值,因此它不需要任何其他内容作为条件。
- 1 回答
- 0 关注
- 60 浏览
添加回答
举报