Measurement units conversion based on country in Woocommerce product page

泪湿孤枕 提交于 2020-01-30 07:38:06

问题


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.


回答1:


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;
}

This code goes on function.php file of your active child theme (or theme). Tested and works.



来源:https://stackoverflow.com/questions/49207367/measurement-units-conversion-based-on-country-in-woocommerce-product-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!