Skip Navigation

[Resolved] Assign custom field to WC order

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

Problem: I would like to use the title of a parent post to set a custom field value using a cred_save_data hook, but I'm not sure how to access the parent post.

Solution: It depends on when this Form was created and whether or not this post relationship has been migrated into the new Post Relationships system. In the old system, you could access the parent post ID in the $_POST superglobal as _wpcf_belongs_concours_id, then get the title using get_the_title:

$meta_value = isset($_POST['_wpcf_belongs_concours_id']) ? get_the_title($_POST['_wpcf_belongs_concours_id']) : '';

In the new system, you can access the parent post using the updated relationship slug syntax. It looks like @relationship-slug_parent, but depends on your post relationship slug:

$meta_value = isset($_POST['@relationship-slug_parent']) ? get_the_title($_POST['@relationship-slug_parent']) : '';
This support ticket is created 6 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Pat 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1174774

Pat

Hello,

I'm using the WooCommerce Admin Custom Order Fields plugin in order to add custom field to order page and order emails.
The plugin is working fine and I can have the desired info on the order page and emails.
My process id the following :

I have a postype "Concours" that is used to create event. When a user is on this page, he has the ability to book a product. For this, I have a specific postype "inscription". This postype is a child of "Concours", so I can retrieve the concours name. When the user finalize his choice thanks to a WC Cred form, a new post inscription is created and a WC order is created (using a specific product defined in the WC Cred form)
Now, I would like to populate the Woocommerce admin custom field after having submitted the Cred form.
The way I can have access to this custom field in the database is :
get_post_meta( $order_id, '_wc_acof_2', true )
So, I suppose I need to create the record in the cred_save_data :

add_action('cred_save_data', 'add_custom_field',10,2);
function add_custom_field($post_id, $form_data)
{
$meta_key = '_wc_acof_2';
$meta_value = ?;
add_post_meta($post_id, $meta_key, $meta_value, true);
}

The only question here is how can I retrieve the title of the concours which is a parent of the created inscription post (I should use _wpcf_belongs_concours_id but I'm not sure of the syntax and how to retrieve it during the save data process) ?
Regards
Pat

#1174895

The only question here is how can I retrieve the title of the concours which is a parent of the created inscription post (I should use _wpcf_belongs_concours_id but I'm not sure of the syntax and how to retrieve it during the save data process) ?
Hi Pat, it depends on when this Form was created and whether or not this post relationship has been migrated into the new Post Relationships system. In the old system, you could access the parent post ID in the $_POST superglobal as _wpcf_belongs_concours_id, then get the title using get_the_title:

$meta_value = isset($_POST['_wpcf_belongs_concours_id']) ? get_the_title($_POST['_wpcf_belongs_concours_id']) : '';

In the new system, you can access the parent post using the updated relationship slug syntax. It looks like @relationship-slug_parent, but depends on your post relationship slug:

$meta_value = isset($_POST['@relationship-slug_parent']) ? get_the_title($_POST['@relationship-slug_parent']) : '';
#1176749

Pat

Hi Christian,

Many thanks for your explanation.
Best wishes for 2019.
Regards
Pat