t( $_POST [‘customer_message’] ) ) {
$custom_data = array() ;
$custom_data [ ‘customer_name’ ] = isset( $_POST [‘customer_name’] ) ? sanitize_text_field ( $_POST [‘customer_name’] ) : \
$custom_data [ ‘customer_message’ ] = isset( $_POST [‘customer_message’] ) ? sanitize_text_field ( $_POST [‘customer_message’] ): \
$cart_item_meta [‘custom_data’] = $custom_data ;
}
return $cart_item_meta;
}
\/**
* Display the custom data on cart and checkout page
*\/
add_filter( ‘woocommerce_get_item_data’, ‘njengah_item_data’ , 25, 2 );
function njengah_item_data ( $other_data, $cart_item ) {
if ( isset( $cart_item [ ‘custom_data’ ] ) ) {
$custom_data = $cart_item [ ‘custom_data’ ];
$other_data[] = array( ‘name’ => ‘Name’,
‘display’ => $ custom_data[‘customer_name’] );
$other_data[] = array( ‘name’ => ‘Message’,
‘display’ => $ custom_data[‘customer_message’] );
}
return $other_data;
}
\/**
* Add order item meta
*\/
add_action( ‘woocommerce_add_order_item_meta’, ‘njengah_order_item_meta’ , 10, 2);
function njengah_order_item_meta ( $item_id, $values ) {
if ( isset( $values [ ‘custom_data’ ] ) ) {
$custom_data = $values [ ‘custom_data’ ];
wc_add_order_item_meta( $item_id, ‘Name’, $custom_data[‘customer_name’] );
wc_add_order_item_meta( $item_id, ‘Message’, $custom_data[‘customer_message’] );
}
}
The results of the product page are as follows: The results of shopping cart page are as follows: The results of the settlement page are as follows: The results of the order details page are as follows: The results of the order editing page are as follows: End today’s tutorial and share the custom solution created to add forms to the product page. The collected details will be displayed throughout the order details described in this post.
However, you should be very careful when editing the core files of the woocommerce store. If an error occurs, an error is displayed. Hopefully, this solution will help you understand how the order lifecycle works. How to change woocommerce checkout error message when migrating from similar article Shopify to woocommerce woocommerce custom order product generation page woocommerce storefont subject product page total width woocommerce how to send email payment page for status change to obtain order ID w How to change oocommerce coupon code placeholder woommerce page store foreground