• Resolved mdawg32

    (@mdawg32)


    I’ve added the below code to my functions.php file in order to record an extra answer from users at the checkout process for my site with WooCommerce. The button was added nicely, but when I look for the entry in the backend I see “Donation Reason” and then it’s blank.

    What am I doing wrong that it’s not recording their answer?

    /**
    * Add the field to the checkout
    **/
    add_action('woocommerce_after_order_notes', 'my_reason_checkout_field');
    
    function my_reason_checkout_field( $checkout ) {
    
    woocommerce_form_field( 'donation_reason', array(
    'type' => 'select',
    'class' => array('my-reason-class form-row-wide'),
    'label'     => __('Reason for donation:', 'woocommerce'),
    'required' => true,
    'options' => array(
      'general' => __('General Donation', 'woocommerce'),
      'annual' => __('Annual Appeal', 'woocommerce'),
      'dining' => __('Dining in the Dark', 'woocommerce'),
      'honor' => __('In Honor Of', 'woocommerce'),
      'memory' => __('In Memory Of', 'woocommerce')
      ),
    ), $checkout->get_value( 'donation_reason' ));
    
    }
    
    /**
    * Process the checkout
    **/
    add_action('woocommerce_checkout_process', 'my_reason_checkout_field_process');
    
    function my_reason_checkout_field_process() {
    global $woocommerce;
    
    // Check if set, if its not set add an error.
    if (!$_POST['donation_reason'])
         $woocommerce->add_error( __('Please select your Donation Reason.') );
    }
    
    /**
    * Update the order meta with field value
    **/
    add_action('woocommerce_checkout_update_order_meta', 'my_reason_checkout_field_update_order_meta');
    
    function my_reason_checkout_field_update_order_meta( $order_id ) {
    if ($POST['donation_reason']) update_post_meta( $order_id, 'myreason', esc_attr($_POST['donation_reason']));
    }
    
    /**
    * Display field value on the order edition page
    **/
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_reason_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_reason_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Donation Reason').':</strong> ' . get_post_meta( $order->id, 'myreason', true ) . '</p>';
    }
Viewing 1 replies (of 1 total)
  • Thread Starter mdawg32

    (@mdawg32)

    Got it, here’s the working code.

    /**
     * Add the field to the checkout
     */
    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        woocommerce_form_field( 'my_field_name', array(
            'type'          => 'select',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Fill in this field'),
            'placeholder'   => __('Enter something'),
            'options' => array(
              'General Donation' => __('General Donation', 'woocommerce'),
              'Annual Appeal' => __('Annual Appeal', 'woocommerce'),
              'Dining in the Dark' => __('Dining in the Dark', 'woocommerce'),
              'In Honor Of' => __('In Honor Of', 'woocommerce'),
              'In Memory Of' => __('In Memory Of', 'woocommerce'),
            ),
        ), $checkout->get_value( 'my_field_name' ));
    
    }
    
    /**
     * Process the checkout
     */
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
        // Check if set, if its not set add an error.
        if ( ! $_POST['my_field_name'] )
            wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
    }
    
    /**
     * 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>'.__('Reason for Donation').':</strong> ' . get_post_meta( $order->id, 'My Field', true ) . '</p>';
    }
Viewing 1 replies (of 1 total)
  • The topic ‘WooCommerce custom drop down button not writing entry to backend’ is closed to new replies.