WooCommerce function.php – how to save coupon code to a custom field?

How to add Custom Product Fields in WooCommerce

I followed the guide above and add the code below to my functions.php. That worked fine. My problem is how to I modify this code to save the coupon code $_POST[‘coupon_code’] entered (during checkout) and save that to the custom field? I am stuck at that point, since my overall goal is to test the coupon code and fill the custom field with the salesperson that promoted that coupon code to the customers.

Read More

Thank you for any help!

/**
 * Add the field to the checkout
 */
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Fill in this field'),
        'placeholder'   => __('Enter something'),
        ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

}

/**
 * Update the order meta with field value
 */
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'] ) ) {
        update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
    }
}

/**
 * Display field value on the order edit page
 */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
}

Related posts

1 comment

  1. If you are still looking for answers, I managed to spice up your code with some help from my own question as I was in a similair situation.

    Fill out custom field with used coupon code WooCommerce

    your code and my edits were changed to where it actually saves the coupons to a custom value. On of the problems with your code was how you tried to reach the custom field with ‘My field’. I have replaced these with the actual name of the field.

    /**
     * Add the field to the checkout
     */
    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
    
        woocommerce_form_field( 'my_field_name', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Fill in this field'),
            'placeholder'   => __('Enter something'),
            ), $checkout->get_value( 'my_field_name' ));
    
        echo '</div>';
    
    }
    
    /**
     * Update the order meta with field value
     */
    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 );
          foreach( $order->get_used_coupons() as $coupon) {
                    $coupons .= $coupon.', ';
                    }
        update_post_meta( $order_id, 'my_field_name', $coupons);
      }
    
    }
    
    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('My Field').':</strong> ' . get_post_meta( $order->id, 'my_field_name', true ) . '</p>';
    }
    

    You might want to clean up a bit before using this code. 😉

    Source: Fill out custom field with used coupon code WooCommerce

Comments are closed.