如何从WooCommerce结帐中保存自定义复选框字段状态?

如何从WooCommerce结帐中保存自定义复选框字段状态?

问题描述:

我对update_post_meta函数有问题.我有一个用户提交的值,我通过$ _POST传递了该值,然后保存了该值.

I have a problem with the update_post_meta function. I have a user submitted value, which I pass via $_POST and then saving to post meta.

一切正常,但是当值为'0'时,发布的meta不会更新.

All is working fine, but when the value is '0' the post meta is not updated.

这是我的代码:

// Add custom checkout field: woocommerce_review_order_before_submit
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_ritiro_sede' );
function my_custom_checkout_field_ritiro_sede() {
    echo '<div class="cw_custom_class"><h3>'.__('Ritiro presso sede CER S.r.l. &nbsp').'</h3>';
    echo '<div id="my_custom_checkout_field">';
    woocommerce_form_field( 'ritiro_sede', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('SI'),
    ),  WC()->checkout->get_value( 'ritiro_sede' ) );
    echo '</div>';
}

// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta_ritiro_sede', 10, 1 );
function custom_checkout_field_update_order_meta_ritiro_sede( $order_id ) {

    if ( ! empty( $_POST['ritiro_sede'] ) )
        update_post_meta( $order_id, 'ritiro_sede', $_POST['ritiro_sede'] );
    if ( isset( $_POST['ritiro_sede'] ) )
        update_post_meta( $order_id, 'ritiro_sede', $_POST['0'] );
    
}

有人知道什么地方可能出问题吗?

Does anyone have any idea what might be wrong?

自WooCommerce 3起,以下是将自定义结帐复选框字段值保存为订单元数据的最佳方法(包括未选中该复选框时):

Since WooCommerce 3, here below is the best way to save your custom checkout checkbox field value as order meta data (including when the checkbox is unchecked):

// Save the custom checkout checkbox field as the order meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
    $value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
    
    $order->update_meta_data( 'ritiro_sede', $value );
}

现在,在以下第一个函数中, WC_Checkout get_value()方法将使用用户元数据:

Now as user meta data is used by WC_Checkout get_value() method in your first function on:

WC()->checkout->get_value( 'ritiro_sede' )

因此,如果要在下次购买的结帐页面上显示提交的值,则需要使用以下命令将该自定义结帐字段另存为用户元数据:

So if you want the submitted value to be displayed on checkout page for the next purchase, you will need to save that custom checkout field also as user meta data using instead the following:

// Save the custom checkout checkbox field as the order meta and user meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
    $value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
    
    // Save as custom order meta data
    $order->update_meta_data( 'ritiro_sede', $value );

    // Save as custom user meta data
    if ( get_current_user_id() > 0 ) {
        update_user_meta( get_current_user_id(), 'ritiro_sede', $value );
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.