Woocommerce 产品页面中基于国家/地区的测量单位转换
Measurement units conversion based on country in Woocommerce product page
我有一个 woocommerce 网站设置为在后端设置中使用公斤和厘米作为尺寸单位。该网站主要销往欧洲,所以不用担心。现在我必须向美国市场销售产品,我被要求配置该站点,以便当美国客户组中的用户登录时,他们可以看到英制测量值,也可以看到公制单位。
有没有一种方法至少可以使用某种转换等以英制显示重量和尺寸,并在数据库中将主要单位保留为千克和厘米?
我到处寻找插件,但找不到对我有帮助的插件。
对于单个产品页面,这是一个具体示例,它将转换测量单位值并为使用英制单位的国家/地区设置正确的测量单位标签。
// For Weight
add_filter( 'woocommerce_format_weight', 'imperial_format_weight', 20, 2 );
function imperial_format_weight( $weight_string, $weight ) {
$country = WC()->customer->get_shipping_country(); // Customer country
$countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries
if ( ! in_array( $country, $countries ) ) return $weight_string; // Exit
$weight_unit = get_option( 'woocommerce_weight_unit' );
$weight_string = wc_format_localized_decimal( $weight );
if ( empty( $weight_string ) )
return __( 'N/A', 'woocommerce' ); // No values
if ( $weight_unit == 'kg' ) {
// conversion rate for 'kg' to 'lbs'
$rate = 2.20462;
$label = ' lbs';
} elseif ( $weight_unit == 'g' ) {
// conversion rate for 'g' to 'oz'
$rate = 0.035274;
$label = ' oz';
}
return round( $weight * $rate, 2 ) . $label;
}
// For Dimensions
add_filter( 'woocommerce_format_dimensions', 'imperial_format_dimensions', 20, 2 );
function imperial_format_dimensions( $dimension_string, $dimensions ) {
$country = WC()->customer->get_shipping_country(); // Customer country
$countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries
if ( ! in_array( $country, $countries ) ) return $dimension_string; // Exit
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
$dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );
if( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' ); // No values
if ( $dimension_unit == 'mm' ) {
// conversion rate for 'mm' to 'inch'
$rate = 0.0393701;
$label = ' in';
} elseif ( $dimension_unit == 'cm' ) {
// conversion rate for 'cm' to 'inch'
$rate = 0.393701;
$label = ' in';
} elseif ( $dimension_unit == 'm' ) {
// conversion rate for 'm' to 'yard'
$rate = 1.09361;
$label = ' yd';
}
$new_dimentions = array();
foreach( $dimensions as $key => $value ){
$new_dimentions[$key] = round( $value * $rate, 2 );
}
return implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) ) ) . $label;
}
此代码在您的活动子主题(或主题)的 function.php 文件中。 已测试并有效。
我有一个 woocommerce 网站设置为在后端设置中使用公斤和厘米作为尺寸单位。该网站主要销往欧洲,所以不用担心。现在我必须向美国市场销售产品,我被要求配置该站点,以便当美国客户组中的用户登录时,他们可以看到英制测量值,也可以看到公制单位。
有没有一种方法至少可以使用某种转换等以英制显示重量和尺寸,并在数据库中将主要单位保留为千克和厘米?
我到处寻找插件,但找不到对我有帮助的插件。
对于单个产品页面,这是一个具体示例,它将转换测量单位值并为使用英制单位的国家/地区设置正确的测量单位标签。
// For Weight
add_filter( 'woocommerce_format_weight', 'imperial_format_weight', 20, 2 );
function imperial_format_weight( $weight_string, $weight ) {
$country = WC()->customer->get_shipping_country(); // Customer country
$countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries
if ( ! in_array( $country, $countries ) ) return $weight_string; // Exit
$weight_unit = get_option( 'woocommerce_weight_unit' );
$weight_string = wc_format_localized_decimal( $weight );
if ( empty( $weight_string ) )
return __( 'N/A', 'woocommerce' ); // No values
if ( $weight_unit == 'kg' ) {
// conversion rate for 'kg' to 'lbs'
$rate = 2.20462;
$label = ' lbs';
} elseif ( $weight_unit == 'g' ) {
// conversion rate for 'g' to 'oz'
$rate = 0.035274;
$label = ' oz';
}
return round( $weight * $rate, 2 ) . $label;
}
// For Dimensions
add_filter( 'woocommerce_format_dimensions', 'imperial_format_dimensions', 20, 2 );
function imperial_format_dimensions( $dimension_string, $dimensions ) {
$country = WC()->customer->get_shipping_country(); // Customer country
$countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries
if ( ! in_array( $country, $countries ) ) return $dimension_string; // Exit
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
$dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );
if( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' ); // No values
if ( $dimension_unit == 'mm' ) {
// conversion rate for 'mm' to 'inch'
$rate = 0.0393701;
$label = ' in';
} elseif ( $dimension_unit == 'cm' ) {
// conversion rate for 'cm' to 'inch'
$rate = 0.393701;
$label = ' in';
} elseif ( $dimension_unit == 'm' ) {
// conversion rate for 'm' to 'yard'
$rate = 1.09361;
$label = ' yd';
}
$new_dimentions = array();
foreach( $dimensions as $key => $value ){
$new_dimentions[$key] = round( $value * $rate, 2 );
}
return implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) ) ) . $label;
}
此代码在您的活动子主题(或主题)的 function.php 文件中。 已测试并有效。