Note that there are some explanatory texts on larger screens.

plurals
  1. PORetun a string (yes/no) from cart form checkbox to wordpress meta box
    text
    copied!<p>Pardon the PHP noob question... I have added a custom checkbox for gift wrapping to my woocommerce cart. I am able to get the value to return to the "orders" page in a custom meta box and in the order summary, but I can only get the value to return as null or "1". I'd like to return a value of "yes" or "no". Thanks in advance for any advice.</p> <hr> <p><strong>HERE'S THE CODE I'M USING IN MY FUNCTIONS.PHP (CUSTOMIZED FROM THE WOOCOMMERCE DOCUMENTATION):</strong></p> <p>/<em>Add Gift Wrapping field to the checkout</em>/</p> <pre><code>add_action('woocommerce_after_order_notes', 'gift_wrapping_field'); function gift_wrapping_field( $checkout ) { woocommerce_form_field( 'gift_wrapping', array( 'type' =&gt; 'checkbox', 'class' =&gt; array('input-checkbox'), 'label' =&gt; __('Include Free Gift Wrapping'), 'required' =&gt; false, ), $checkout-&gt;get_value( 'gift_wrapping' )); } </code></pre> <p>/<em>Process the checkout</em>/</p> <pre><code>add_action('woocommerce_checkout_process', 'gift_wrapping_field_process'); function my_custom_checkout_field_process() { global $woocommerce; // Check if set, if its not set add an error. if (!$_POST['terms_conditions']) $woocommerce-&gt;add_error( __('Please agree to terms and conditions.') ); } </code></pre> <p>/<em>Update the order meta with field value</em>/</p> <pre><code>add_action('woocommerce_checkout_update_order_meta', 'gift_wrapping_field_update_order_meta'); function gift_wrapping_field_update_order_meta( $order_id ) { if ($_POST['gift_wrapping']) update_post_meta( $order_id, 'Include Free Gift Wrapping', esc_attr($_POST['gift_wrapping'])); } /*Update the order summary with field value*/ 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 '&lt;p&gt;&lt;strong&gt;'.__('Include Free Gift Wrapping').':&lt;/strong&gt; ' . $order-&gt;order_custom_fields['Include Free Gift Wrapping'][0] . '&lt;/p&gt;'; } </code></pre> <hr> <p><strong>AND HERE'S A SCREENSHOT OF WHAT'S BEING RETURNED TO THE ADMIN FROM THE FORM RESULTS:</strong></p> <p><img src="https://i.stack.imgur.com/Y0MZu.png" alt="custom fields screenshot"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload