Question

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.


HERE'S THE CODE I'M USING IN MY FUNCTIONS.PHP (CUSTOMIZED FROM THE WOOCOMMERCE DOCUMENTATION):

/Add Gift Wrapping field to the checkout/

add_action('woocommerce_after_order_notes', 'gift_wrapping_field');

function gift_wrapping_field( $checkout ) 
{

woocommerce_form_field( 'gift_wrapping', array(
    'type'          => 'checkbox',
    'class'         => array('input-checkbox'),
    'label'         => __('Include Free Gift Wrapping'),
    'required'        => false,
    ), $checkout->get_value( 'gift_wrapping' ));

}

/Process the checkout/

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->add_error( __('Please agree to terms and conditions.') );
}

/Update the order meta with field value/

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 '<p><strong>'.__('Include Free Gift Wrapping').':</strong> ' . $order->order_custom_fields['Include Free Gift Wrapping'][0] . '</p>';
}

AND HERE'S A SCREENSHOT OF WHAT'S BEING RETURNED TO THE ADMIN FROM THE FORM RESULTS:

custom fields screenshot

Était-ce utile?

La solution

Try changing this

esc_attr($_POST['gift_wrapping'])

To this

(esc_attr($_POST['gift_wrapping'] = 1 ? 'yes' : ''))

I have tested this on a woocommerce test site I have and working for me.

This will return yes if ticked and nothing if unticked. If you are dead set on having no, then you can change the whole line from this

if ($_POST['gift_wrapping']) update_post_meta( $order_id, 'Include Free Gift Wrapping', esc_attr($_POST['gift_wrapping']));

To this. Which will force the save of a value even if the value has not technically been set.

update_post_meta( $order_id, 'My Field', (esc_attr(isset($_POST['gift_wrapping']) && $_POST['gift_wrapping'] == 1 ? 'yes' : 'no')));

Autres conseils

To show a 'no' maybe try this

function gift_wrapping_field_update_order_meta( $order_id ) 
{
    $gift_wrapping = 'no';
    if ($_POST['gift_wrapping']) {
        $gift_wrapping = 'yes';
        update_post_meta( $order_id, 'Include Free Gift Wrapping', esc_attr($gift_wrapping) );
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top