Ecommerce Shopify WordPress Discussion

Woocommerce: show custom product data in checkout review order and invoice

The price of the products is calculated in the shop based on a field "Length" which the user submits on the product page. As this length is important information I want to show the submitted length also in the cart, review order in checkout and invoices. I have succesfully added the field to the cart, but how can I add it to the cart items in the review order and invoices? (Or even in every product mention in the whole process). Is there a filter or action I can use? And how? I could not find any? I have the following (relevant) code: // Get custom field value Length, calculate new item price, save it as custom cart item data add_filter('woocommerce_add_cart_item_data', 'add_custom_field_data', 20, 2 ); function add_custom_field_data( $cart_item_data, $product_id ){ if (! isset($_POST['length'])) return $cart_item_data; $length = (float) sanitize_text_field( $_POST['length'] ); if( empty($length) ) return $cart_item_data; $product = wc_get_product($product_id); // The WC_Product Object $base_price = (float) $product->get_regular_price(); // Product reg price // New price calculation $new_price = (($base_price * $length)/1000)+1.90; // Set the custom amount in cart object $cart_item_data['custom_data']['extra_charge'] = (float) $length; $cart_item_data['custom_data']['new_price'] = (float) $new_price; $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique return $cart_item_data; } // Set the new calculated cart item price add_action( 'woocommerce_before_calculate_totals', 'new_price_add_length', 20, 1 ); function new_price_add_length( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; foreach ( $cart->get_cart() as $cart_item ) { if( isset($cart_item['custom_data']['new_price']) ) $cart_item['data']->set_price( (float) $cart_item['custom_data']['new_price'] ); } } //Add length to product in cart add_action( 'woocommerce_after_cart_item_name', 'wmw_display_length', 20 ); function wmw_display_length( $cart_item ) { if( isset($cart_item['custom_data']['extra_charge']) ) { $length = $cart_item['custom_data']['extra_charge']; echo '<dl class="variation"><dt class="variation-Length">Length(mm):</dt><dd class="variation-Length"><p>' . $cart_item['custom_data']['extra_charge'] . '</dd></dl>'; } } Any ideas how to get this data also in the other locations/screens?
Update For testing purpose only, as you didn't provide the code that displays a "length" input field in single product pages, I use the following function: // FOR testing: Display length field in single product pages add_action('woocommerce_before_add_to_cart_button', 'testing_custom_product_input_field', 5 ); function testing_custom_product_input_field() { woocommerce_form_field('length', array( 'class' => array( 'form-row-left') , 'label' => __('Length(mm)', 'woocommerce' ), 'placeholder' => __('Define the required Length (in mm)') , 'required' => true, ) , ''); } Then I have simplified and make changes to your provided code, so now the "length" is displayed in cart mini-cart and checkout pages. Use the following code replacement: // Get custom field value Length, calculate new item price, save it as custom cart item data add_filter('woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 20, 2 ); function add_custom_cart_item_data( $cart_item_data, $product_id ){ if ( isset($_POST['length']) && ! empty($_POST['length']) ) { $product = wc_get_product($product_id); // Get WC_Product Object $length = (float) sanitize_text_field( $_POST['length'] ); $price = ( floatval($product->get_regular_price()) * $length / 1000 ) + 1.90; // New price calculation $cart_item_data['custom_data']['length'] = $length; $cart_item_data['custom_data']['price'] = $price; $cart_item_data['custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique } return $cart_item_data; } // Display custom cart item data in mini cart, Cart and Checkout pages add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 20, 2 ); function display_custom_cart_item_data( $cart_data, $cart_item ) { if( isset($cart_item['custom_data']['length']) ) { $cart_data[] = array( 'key' => __('Length(mm)', 'woocommerce'), 'value' => $cart_item['custom_data']['length'], ); } return $cart_data; } // Set the new calculated cart item price add_action( 'woocommerce_before_calculate_totals', 'new_price_add_length', 20, 1 ); function new_price_add_length( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; foreach ( $cart->get_cart() as $cart_item ) { if( isset($cart_item['custom_data']['price']) ) $cart_item['data']->set_price($cart_item['custom_data']['price']); } } Add the following missing code, that handles mini-cart displayed custom price: // Handle mini cart displayed price add_filter( 'woocommerce_cart_item_price', 'display_new_price_add_length', 20, 2 ); function display_new_price_add_length( $price_html, $cart_item ) { if( isset($cart_item['custom_data']['price']) ) { $args = array( 'price' => floatval($cart_item['custom_data']['price']) ); if ( WC()->cart->display_prices_including_tax() ) { $product_price = wc_get_price_including_tax( $cart_item['data'], $args ); } else { $product_price = wc_get_price_excluding_tax( $cart_item['data'], $args ); } return wc_price( $product_price ); } return $price_html; } To finish, add the following to display your "Length" on customer orders, admin orders and email notifications and invoices: // Save and display the Length (custom order item metadata) add_action( 'woocommerce_checkout_create_order_line_item', 'save_order_item_custom_meta_data', 10, 4 ); function save_order_item_custom_meta_data( $item, $cart_item_key, $values, $order ) { if( isset($values['custom_data']['length']) ) { $item->update_meta_data( 'length', $values['custom_data']['length'] ); } } // Add readable "meta key" label name replacement add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 10, 3 ); function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) { if( $item->get_type() === 'line_item' && $meta->key === 'length' ) { $display_key = __('Length(mm)', 'woocommerce' ); } return $display_key; } Code goes in functions.php file of your child theme (or in a plugin). Tested and work. Checkout page: Customer order: Admin order: Email notification:

January 20, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.