【问题标题】:Measurement units conversion based on country in Woocommerce product pageWoocommerce 产品页面中基于国家/地区的测量单位转换
【发布时间】:2018-03-10 09:15:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce product measurement


    【解决方案1】:

    对于单个产品页面,以下是一个具体示例,它将转换测量单位值并为使用英制单位的国家/地区设置正确的测量单位标签。

    // 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 文件中。经过测试并且有效。

    【讨论】:

    • 您好 - 好的,我测试了代码,当客户送货地址设置为美国时效果很好。我现在的问题是,当英国或任何使用公制的客户访问该网站时,维度根本不会显示。有没有办法根据客户的地理位置同时显示英制或公制?
    • @JamesDonald 是的,这是可能的……如果您不知道该怎么做,您应该提出一个标记为“woocommerce”的新问题,并在完成后在此处通知我。在我更新的实际代码中也有一个小错误。
    • 了解提问-道歉-感谢修改代码-现在一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多