Woocommerce 产品页面中基于国家/地区的计量单位转换 [英] Measurement units conversion based on country in Woocommerce product page

查看:75
本文介绍了Woocommerce 产品页面中基于国家/地区的计量单位转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 woocommerce 网站设置为在后端设置中使用公斤和厘米作为尺寸单位.该网站主要销往欧洲,所以不用担心.现在我必须向美国市场销售产品,并且我被要求配置该站点,以便当美国客户组中的用户登录时,他们会看到英制单位以及或代替公制单位.

I have a woocommerce site set to use Kg and cm as dimension units in the backend settings. The site mainly sells to Europe so no worries. Now I have to sell to US market to and I am being asked to configure the site so that when users in the US-Client group login they see Imperial measurements either as well as or instead of Metric units.

有没有办法至少我可以使用一些转换等以英制显示重量和尺寸,并在数据库中将主要单位保持为公斤和厘米?

Is there a way at least I can display the weight and dimensions in Imperial using some conversion etc and keep primary units as Kg and cm in the database?

我到处寻找插件,但找不到对我有帮助的插件.

I've looked everywhere for a plugin but cant find one that helps me.

推荐答案

对于单个产品页面,这里有一个具体示例,它将转换度量单位值并为使用英制单位的国家/地区设置正确的度量单位标签.

For single product pages, Here is a concrete example that will convert measurement unit values and set the correct measurement unit label for countries that use Imperial units.

// 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 产品页面中基于国家/地区的计量单位转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆