based on Save product custom-field as custom order item metadata for WooCommerce admin manual orders
I have these codes that show the weight and dimensions of the product for orders placed from the frontend, but I can't get them to work for orders that are placed manually from the backend. When I add the products I do not see the weight and dimensions and they are not shown on the page to pay the order, in order-received and email.
That is my code:
Show and Save the weight of the product everywhere.
// Display the cart item weight in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( $cart_item['data']->get_weight() > 0 ){
$cart_item_data[] = array(
'name' => __( 'Weight', 'woocommerce' ),
'value' => $cart_item['data']->get_weight() . ' ' . get_option('woocommerce_weight_unit')
);
}
return $cart_item_data;
}
// Save and Display the order item weight (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
if ( $values['data']->get_weight() > 0 ){
$item->update_meta_data( __( 'Weight', 'woocommerce' ), $values['data']->get_weight() . ' ' . get_option('woocommerce_weight_unit') );
}
}
Show and Save product dimensions everywhere.
// Display the cart item weight in dimensions and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data1', 10, 2 );
function display_custom_item_data1( $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_data1', 20, 4 );
function display_order_item_data1( $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') );
}
}
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_dimentions = array(
__('Largo:', $text_domain) => $dimensions['length'],
__('Ancho:', $text_domain) => $dimensions['width'],
__('Alto:', $text_domain) => $dimensions['height'],
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );
foreach( $dimensions as $key => $dimention ){
$dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . get_option( 'woocommerce_dimension_unit' );
}
return implode( ' × ', $dimensions);
}