Добавить другие отображаемые размеры и весовые единицы в Woocommerce - PullRequest
0 голосов
/ 02 сентября 2018

Может ли кто-нибудь поделиться кодом функции, чтобы в Woocommerce была другая единица измерения, кроме веса и измерения?

Пример типа изображения ниже

enter image description here

1 Ответ

0 голосов
/ 02 сентября 2018

Обновление 2 (март 2019 г.)

Следующий код добавится к отображаемым по умолчанию размерам и значениям дополнительных преобразованных единиц веса, если это необходимо:

add_filter( 'woocommerce_format_weight', 'custom_format_weight', 20, 2 );
function custom_format_weight( $weight_string, $weight ){
    // Format decimals from default weight value
    $weight_string  = wc_format_localized_decimal( $weight );
    // Format decimals from custom converted weight value
    $weight_string2 = wc_format_localized_decimal( round($weight * 0.45359237, 2) );

    if ( ! empty( $weight_string ) ) {
        $weight_string2 = ' ( ' . $weight_string2 . ' ' . __( 'kg', 'woocommerce' ) . ' )';
        $weight_string .= ' ' . get_option( 'woocommerce_weight_unit' ) . $weight_string2;
    } else {
        $weight_string = __( 'N/A', 'woocommerce' );
    }

    return $weight_string;
}

add_filter( 'woocommerce_format_dimensions', 'custom_format_dimensions', 10, 2 );
function custom_format_dimensions( $dimension_string, $dimensions ){
    // Initializing variable
    $dimentions2 = array();

    // Loop though dimensions array (and set custom converted formatted decimals values in a new array)
    foreach( $dimensions as $key => $value ){
        if( ! empty($value) && $value != 0 )
            $dimentions2[$key] = wc_format_localized_decimal( round($value * 2.54, 2) );
    }

    // Format default dimentions in a string
    $dimension_string  = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );

    if ( ! empty( $dimension_string ) ) {
        // Format custom converted array in a string and append it to default formatted dimensions string
        $dimension_string2 = ' ( ' . implode( ' x ', $dimentions2 ) . ' ' . __( 'cm', 'woocommerce' ) . ' )';
        $dimension_string .= ' ' . get_option( 'woocommerce_dimension_unit' ) . $dimension_string2;
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }

    return $dimension_string;
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы). проверено и работает.

enter image description here

...