Skip Navigation

[Resolved] How is possible to save it in the woocommerce cart meta?

This thread is resolved. Here is a description of the problem and solution.

Problem:

Using Toolset form commerce to submit the form and post custom fields, should be great to have the possibility to call this data in the Woocommerce cart page.

My partner don't have access to the backend and to my-order custom post, but only to the detail of woocommerce order.

Solution:

You can consider the Woocommerce filter hook woocommerce_cart_item_name, see details here:

https://toolset.com/forums/topic/how-is-possible-to-save-it-in-the-woocommerce-order-meta/page/2/#post-1117554

Relevant Documentation:

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 22 replies, has 2 voices.

Last updated by domenicoS 6 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1117043
sreenshot-01-01-01.jpg
sreenshot-01-01.jpg
sreenshot-01.jpg

Yes Luo, I see the field as you explain. But I need to see this data in the woocommerce meta for the reason that I explain at the beginning of this post. My partner who manage the product cannot have access to the backend.

So is important to display this meta in the woocommerce meta in the frontend and also in the backend.

A share with you screenshots to see where data need to be displayed.

Hope it helps

#1117064

That is completed different problem, see your original question:
https://toolset.com/forums/topic/how-is-possible-to-save-it-in-the-woocommerce-order-meta/#post-1115456
You are asking for the meta fields of order post, so I setup the demo for updating custom fields of order post

But those screenshots you mentioned above are in Woocommerce cart page, in the cart page, the woocommerce order post isn't created.

In your case, it needs other custom codes, I searched it in google, find this thread:
hidden link

See sections "Adding custom values to the cart" and "Displaying custom fields in the cart and checkout"
hidden link
hidden link

#1117077

My qustion was this:
"Data as number of people and number of child, ecc ar very important. How is possible to save it in the woocommerce order meta? should be great to have the possibility to call this data in the order detail and in the order email which my partner received automaticcally by woocommerce. My partner don't have access to the backend and to my-order custom post, but only to the detail of woocommerce order. At the moment in this detail there is only the final price, they cannot know how many adults, childs acc."

And i was reffering to the order post meta from the beginning.

I tried with this code ad in the example you shared with me, but I'm not able to see the adult number as in the screenshot:

function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
 if( ! empty( $_POST['wpcf-adult-number'] ) ) {
 // Add the item data
 $cart_item_data['title_field'] = $_POST['wpcf-adult-number'];
 }
 return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'cfwc_add_custom_field_item_data', 10, 4 );

function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
 if( isset( $cart_item['wpcf-adult-number'] ) ) {
 $name .= sprintf(
 '<p>%s</p>',
 esc_html( $cart_item['wpcf-adult-number'] )
 );
 }
 return $name;
}
add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );
#1117554

I have added below codes in your theme file "functions.php":

/**
 * Display the custom field value in the cart
 */
function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
	if($cart_item['cred_meta']['cred_form_id'] != 24662 || !isset($cart_item['cred_meta']['cred_post_id']))return;  //24662 is the Toolset form ID
	$post_id = $cart_item['cred_meta']['cred_post_id'];
	$adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
	
	$name .= sprintf(
		'<p>%s</p>',
		esc_html( 'Adult number: ' . $adult_num )
	);
 return $name;
}
add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );

Please test again, and it is only a demo for your reference.

#1117557
sreenshot-01-01-01.jpg

Hi, is perfect, thanks.
Just the last thing. How add the meta also to the order as in the image?
Now is added only to the cart.

Thank you Luo for your great support

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/how-add-the-meta-also-to-the-woocommerce-order/

#1118100

I assume the original question of this thread has been resolved:
https://toolset.com/forums/topic/how-is-possible-to-save-it-in-the-woocommerce-order-meta/

For the new question, please check the new thread here:
https://toolset.com/forums/topic/how-add-the-meta-also-to-the-woocommerce-order/

#1119659

My issue is resolved now. Thank you!

#1130615

Hi Luo,

I've to add the same filter with another id-form.

The original is:

	if($cart_item['cred_meta']['cred_form_id'] != 24662 || !isset($cart_item['cred_meta']['cred_post_id']) ) || return $name;

How I've to change the code? I tryed wuth this, but is does not works

	if($cart_item['cred_meta']['cred_form_id'] != 24662 || $cart_item['cred_meta']['cred_form_id'] != 25474 || !isset($cart_item['cred_meta']['cred_post_id']) ) || return $name;