Skip Navigation

[Resolved] Get product custom field data and save to order custom field

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

Problem:
Get product custom field data and save to order custom field

Solution:
Assuming that you are Toolset form's commerce then, Toolset Form's commerce offers the hook "cred_commerce_after_order_completed".
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

With this hook, it has the product_id and based on the product ID, you can get the value of your custom field and it also offers the psot_id. So, what if you try to use the hook and based on the product_id get the value of your desired custom field and then assign it to your post based on the post_id.

Relevant Documentation:

This support ticket is created 2 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by Rita 2 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2291239

Hi there

I am using Toolset and Woocommerce and currently have a little function working nicely to save data in a custom field when an order is created. This is that snippet:

add_action( 'save_post', 'set_order_status_value', 100, 3);
function set_order_status_value ( $post_id, $post, $update ) {
$field_slug = 'CUSTOM-FIELD-SLUG';
$forced_value = 'TEXT-TO-SAVE-IN-THIS-CUSTOM-FIELD';
$post_type_slug = 'shop_order';
$post_type = get_post_type( $post_id );
if ( $post_type !== $post_type_slug || $update ) {
return;
}
update_post_meta( $post_id, 'wpcf-' . $field_slug, $forced_value );
}

However, I would like to copy across to the order a custom field value of the product being purchased instead of a specified static value on this line:

$forced_value = 'TEXT-TO-SAVE-IN-THIS-CUSTOM-FIELD';

Is this something you can help me with? I am struggling to make my code work...

Much appreciated if so!

Rita

#2291741

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

However, I would like to copy across to the order a custom field value of the product being purchased instead of a specified static value on this line:
$forced_value = 'TEXT-TO-SAVE-IN-THIS-CUSTOM-FIELD';
==>
Could you please share bit more details as its not clear to me what value you want to assign at and when. When you say copy across to the order, what you mean, do you mean that you have added multiple products to single order and then you want to assign some value?

#2291909

Hi Minesh

Good point re multiple products. So, whether there are multiple 'simple' products in the order or not, I have a custom field in all products and, if it is NOT empty, then copy or add the value of that custom field to a custom field in the order. Does that make sense?

#2291927

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Its still not clear to me what value you would like to copy of what field to what field.

Do you mean that if the value of your custom field is empty, you want to copy the value for that custom field from the existing order. So, if the custom field is empty, we should search for any existing order and get the field value and assign it to this new post. is this correct?

#2292003

Hi Minesh

Ok so let's say we have a woo product post that includes a custom field - [types field='PRODUCT-CUSTOM-FIELD'][/types] - which is sometimes empty and sometimes not depending on the products in the cart at checkout.
As usual, on the product post the customer chooses the number of items, goes to cart and then to checkout.
On successful payment the order is created and notification sent to the customer.
At this moment of successful payment and creating the order I would like to save the value of that custom field from the product if the field is NOT empty to a specified custom field in the order - [types field='ORDER-CUSTOM-FIELD'][/types].

The code I have above does exactly this except it specifies what value to save to the order's field.
I want this code to get the value from the products in the order...

So. Save the product's custom field value - [types field='PRODUCT-CUSTOM-FIELD'][/types]
to an existing custom field belonging to the order - [types field='ORDER-CUSTOM-FIELD'][/types]

Does that make sense?

#2292833

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Assuming that you are Toolset form's commerce then, Toolset Form's commerce offers the hook "cred_commerce_after_order_completed".
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

With this hook, it has the product_id and based on the product ID, you can get the value of your custom field and it also offers the psot_id. So, what if you try to use the hook and based on the product_id get the value of your desired custom field and then assign it to your post based on the post_id.

#2299683

Thanks Minesh. I will try to figure that out. And close the ticket meanwhile.

#2299689

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.