WooCommerce custom drop down button not writing entry to backend
-
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)
Viewing 1 replies (of 1 total)
- The topic ‘WooCommerce custom drop down button not writing entry to backend’ is closed to new replies.