Skip Navigation

[Resolved] Set the value of a toolset custom field when a woocommerce order is created

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

Problem: I would like to set the value of a custom field automatically whenever a WooCommerce Order is created from the front-end of the site.

Solution: Use a custom code snippet with the save_post hook to programmatically set the custom field value:

function tssupp_set_select_value_1 ( $post_id, $post, $update ) {
  $field_slug = 'custom-field-slug';
  $forced_value = '1';
  $post_type_slug = 'shop_order';
 
  // you should not edit anything below this line
 
  // - bail on wrong post type or on update
  $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 );
}
 
// 1688213: Automatically select custom field value when order is created
add_action( 'save_post', 'tssupp_set_select_value_1', 100, 3);
This support ticket is created 4 years, 4 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
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)

Author
Posts
#1688213

Hi there

I would like to be able to set the value of a toolset custom field, assigned to the woocommerce order post, in the moment the order is created. I think I need something like a save to post action in php functions? The custom field is a select field with the values: 1, 2, 3, 4 and I would like the new order save the value of '1' when it is created.

Is this possible? Maybe its not possible... I can't seem to find anyone else in our support community asking this same question...

Thanks in advance!

Rita

#1689361

Hello, yes you can use the WordPress hook save_post to set a custom field value automatically when an Order is created from the front-end of the site. This hook is documented here: https://developer.wordpress.org/reference/hooks/save_post/

I have some example code you can use. Replace custom-field-slug with the slug of your custom field as defined in wp-admin (do not include the wpcf- prefix here), then place this code in your child theme's functions.php file or in a new custom code snippet in Toolset > Settings > Custom Code:

function tssupp_set_select_value_1 ( $post_id, $post, $update ) {
  $field_slug = 'custom-field-slug';
  $forced_value = '1';
  $post_type_slug = 'shop_order';

  // you should not edit anything below this line

  // - bail on wrong post type or on update
  $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 );
}

// 1688213: Automatically select custom field value when order is created
add_action( 'save_post', 'tssupp_set_select_value_1', 100, 3);

Let me know if you have questions about this.

#1689473

Thank you Christian!

That works beautifully.

Rita