亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 中向客戶訂單備注添加自定義結帳字段值

在 WooCommerce 中向客戶訂單備注添加自定義結帳字段值

PHP
湖上湖 2023-04-21 14:07:50
簡而言之:我想在 Woocommerce Checkout“daypart”上顯示一個新字段。這是一個有 3 個選項的下拉字段。代碼正在運行,我可以在 WooCommerce 后端看到它,但我需要將數據保存在“客戶備注”字段中,但作為客戶評論(如果有的話)的補充,我被困在這里,不知道如何做到這一點.附加信息:我需要將時段保存在核心結帳字段中,因為商店同步到另一個不同步訂單元數據的運輸服務。這是代碼://define current user status to display things on checkoutif( current_user_can( 'rethunk_retailers' ) ){  // Retailers text    //* Add select field to the checkout pageadd_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' ); }
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

要將“時段”選定值包含到客戶訂單備注中,您將從代碼中替換:


// Update the order meta with field value

add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');

function wps_select_checkout_field_update_order_meta( $order_id ) {


    if ( $_POST['daypart']) update_post_meta( $order_id, 'daypart', esc_attr($_POST['daypart']));

}

通過以下方式:


// Update the order meta with field value

add_action( 'woocommerce_checkout_create_order', 'wps_update_order_meta', 20, 2 );

function wps_update_order_meta( $order, $data ) {

    if ( isset($_POST['daypart']) && ! empty($_POST['daypart']) ) {

        // Add "daypart" selected value as custom order meta data

        $order->update_meta_data('daypart', esc_attr($_POST['daypart']));


        // Get customer note

        $customer_note = isset( $data['order_comments'] ) ? $data['order_comments'] . ' ' : '' );


        // add to existing customer note the "daypart" selected value

        $order->set_customer_note( $customer_note . esc_attr($_POST['daypart']) );

    }

}


查看完整回答
反對 回復 2023-04-21
  • 1 回答
  • 0 關注
  • 219 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號