猿问

在 WooCommerce 中向客户订单备注添加自定义结帐字段值

简而言之:

  1. 我想在 Woocommerce Checkout“daypart”上显示一个新字段。这是一个有 3 个选项的下拉字段。

  2. 代码正在运行,我可以在 WooCommerce 后端看到它,但我需要将数据保存在“客户备注”字段中,但作为客户评论(如果有的话)的补充,我被困在这里,不知道如何做到这一点.

附加信息:

我需要将时段保存在核心结帐字段中,因为商店同步到另一个不同步订单元数据的运输服务。

这是代码:

//define current user status to display things on checkout

if( current_user_can( 'rethunk_retailers' ) ){

  // Retailers text

    //* Add select field to the checkout page

add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');

function wps_add_select_checkout_field( $checkout ) {


    echo '<h2>'.__('Partial Shipment Options').'</h2>';

    echo '<p>'.__('Option 1: details for option 1').'</p>';

    echo '<p>'.__('Option 2: details for option 2').'</p>';

    echo '<p>'.__('Option 3: details for option 3').'</p>';


    woocommerce_form_field( 'daypart', array(

        'label'         => __( 'Delivery options' ),

        'type'          => 'select',

        'class'         => array( 'wps-drop' ),

        'required' => true,

        'options'       => array(

            'blank'     => __( 'Please choose one', 'wps' ),

            'Yes I want Partial Shipment'   => __( 'Option 1: I want a partial shipment', 'wps' ),

            'Please do not ship a partiel shipment' => __( 'Option 2: Do not ship a partial shipment to me.', 'wps' ),

            'I will Wait do not refund anything'    => __( 'Option 3: Do not refund anything', 'wps' )

        )

),

        $checkout->get_value( 'daypart' ));


}


    //* Process the checkout

 add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');

 function wps_select_checkout_field_process() {

    global $woocommerce;


    // Check if set, if its not set add an error.

    if ($_POST['daypart'] == "blank")

     wc_add_notice( '<strong>Please select a Partial Shipment Option </strong>', 'error' );


 }


湖上湖
浏览 187回答 1
1回答

心有法竹

要将“时段”选定值包含到客户订单备注中,您将从代码中替换:// Update the order meta with field valueadd_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');function wps_select_checkout_field_update_order_meta( $order_id ) {&nbsp; &nbsp; if ( $_POST['daypart']) update_post_meta( $order_id, 'daypart', esc_attr($_POST['daypart']));}通过以下方式:// Update the order meta with field valueadd_action( 'woocommerce_checkout_create_order', 'wps_update_order_meta', 20, 2 );function wps_update_order_meta( $order, $data ) {&nbsp; &nbsp; if ( isset($_POST['daypart']) && ! empty($_POST['daypart']) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Add "daypart" selected value as custom order meta data&nbsp; &nbsp; &nbsp; &nbsp; $order->update_meta_data('daypart', esc_attr($_POST['daypart']));&nbsp; &nbsp; &nbsp; &nbsp; // Get customer note&nbsp; &nbsp; &nbsp; &nbsp; $customer_note = isset( $data['order_comments'] ) ? $data['order_comments'] . ' ' : '' );&nbsp; &nbsp; &nbsp; &nbsp; // add to existing customer note the "daypart" selected value&nbsp; &nbsp; &nbsp; &nbsp; $order->set_customer_note( $customer_note . esc_attr($_POST['daypart']) );&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答