Display and save formated dimensions of the product everywhere

血红的双手。 提交于 2021-01-28 15:27:36

问题


Inspired by Display the weight of cart and order items in WooCommerce @LoicTheAztec's answer, making some changes by replacing product weight by product dimensions, I am able to show the items dimensions everywhere on cart items and order items.

The problem is that I don't know how to show the labels or adjectives:

Longitud cm x Ancho cm x Alto cm for a Spanish site.

I really appreciate any help.

// Display the cart item dimensions in cart and checkout pages 
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 ); 
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) { 
    if ( $cart_item['data']->has_dimensions() > 0 ){ 
        $cart_item_data[] = array( 'name' => __( 'Dimensions', 'woocommerce' ), 'value' => $cart_item['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') ); 
    } 
    return $cart_item_data; 
}

// Save and Display the order item dimensions (everywhere) 
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 20, 4 ); 
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) { 
    if ( $values['data']->has_dimensions() > 0 ){ 
        $item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $values['data']->get_dimensions() . ' ' . get_option('woocommerce_dimensions_unit') ); 
    } 
}

回答1:


First the method get_dimensions() has an option to display formatted dimensions, so you can change dimension formatting using the following, to add dimension labels as desired:

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
    $text_domain    = 'woocommerce';
    $dimension_unit = get_option('woocommerce_dimensions_unit');

    if ( empty( $dimension_string ) )
        return __( 'N/A', $text_domain );

    // Set here your new array of dimensions based on existing keys/values
    $new_dimensions = array(
        __('width', $text_domain)  => $dimensions['width'],
        __('length', $text_domain) => $dimensions['length'],
        __('height', $text_domain) => $dimensions['height'],
    );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );

    // Loop through new dimensions array adding spanish labels 
    foreach( $dimensions as $key => $dimention ){
        $dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . $dimension_unit;
    }

    return implode( ' × ',  $dimensions);
}

For Spanish language, you can translate strings with Loco plugin or you can replace:

$new_dimensions = array(
    __('width', $text_domain)  => $dimensions['width'],
    __('length', $text_domain) => $dimensions['length'],
    __('height', $text_domain) => $dimensions['height'],
);

by:

$new_dimensions = array(
    __('longitud', $text_domain) => $dimensions['width'],
    __('ancho', $text_domain)    => $dimensions['length'],
    __('alto', $text_domain)     => $dimensions['height'],
);

Now on your code you will use get_dimensions( true ) method to display formatted dimensions as follow:

// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
    $product =  $cart_item['data'];

    if ( $product->has_dimensions() ){
        $cart_item_data[] = array(
            'name' => __( 'Dimensions', 'woocommerce' ),
            'value' => $product->get_dimensions( true ),
        );
    }
    return $cart_item_data;
}

// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 10, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
    $product =  $values['data'];

    if ( $product->has_dimensions() ){
        $item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $product->get_dimensions( true ) );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Note: As has_dimensions() is a conditional method returning a boolean value, it doesns't need anything else as a condition.

Similar: Rename Length to Diameter in WooCommerce formatted product dimensions output



来源:https://stackoverflow.com/questions/63255270/display-and-save-formated-dimensions-of-the-product-everywhere

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