Fill out custom field with used coupon code WooCommerce


Right now I'm working with a mailchimp plugin that needs a custom field for validating/segmenting.

For this segment I want to check what kind of coupon is used. So I scavenged the following code that should fill my custom field with the used coupons:

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' ); function my_custom_checkout_field_update_order_meta( $order_id ) { if ( empty( $_POST['my_field_name'] ) ) { $coupons = $order->get_used_coupons(); update_post_meta( $order_id, 'my_field_name', $coupons); } }

Sadly this will only crash my site.

Any help would be greatly appreciated.


There are several problems with your code:

<ul><li>You're not validating if the field has any information, so you need to check if $_POST has the my_field_name key</li> <li>You need to load the $order variable in order to get the used coupons.</li> <li>What happens when there's a my_field_value? Do you store it?</li> </ul>

Here's the modified code:

add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' ); function my_custom_checkout_field_update_order_meta( $order_id, $posted ) { if ( isset($_POST['my_field_name']) && empty( $_POST['my_field_name'])) { $order = new WC_Order( $order_id ); $coupons = $order->get_used_coupons(); update_post_meta( $order_id, 'my_field_name', $coupons); } }


  • Is there a way to add a folder to existing 7za archive?
  • putMapping elasticsearch index not found exception
  • Stop a loop inside a method in C#
  • Adding/Removing a webapp to an embedded Jetty
  • Where to find information about Nautilus D-Bus interface
  • Where do the server's and the app's messages display in Intellij 2016?
  • how to display selected songs from ipodLibrary played from avplayer in uitableview
  • Why does specifying Map's initial capacity cause subsequent serializations to give different re
  • Detect which column is showing an editing control in a datagridview
  • Alternative to cvs2svn for performing cvs to svn migration
  • React: parent component props in child without passing explicitly
  • Bitwise right shift >> in Objective-C
  • Auto size a jQuery UI dialog in Internet Explorer
  • How to download models and weights from tensorflow.js
  • Creating an Array of values from DB and passing into a Form
  • Solr facet pagination
  • Apple Push Notifications - App store submission
  • Can I get entire i18n labels of specific dictionary
  • AWS EC2 tomcat permission denied creating/writing to file
  • How to fix Invalid JWT with JHipster Registry [Docker]?
  • Error when build LineageOS: “make: *** [ninja_wrapper] Error 1”
  • How to change WebBrowser fullscreen video mode?
  • mssql script data insert or update
  • Load php page results into div with ajax with link?
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • How can i move Clearcase dyamic/snapshot views to another host (Linux)
  • Google Spreadsheet Script to Blink a range of Cells
  • Terminal run dalvikvm with am.jar
  • How to handle div that is created dynamically in a table
  • Make checkout phone field optional for specific countries in WooCommerce
  • ReferenceError: TextEncoder is not defined