给 WooCommerce 的订单页面添加自定义字段其实这也算是一个常见需求,有些人喜欢一碰到这类需求就找插件实现,其实大可不必。在 WordPress 里面找对钩G m - P j ` 0 * (子,添加对应的代码段就行了。
今天刚完成一个小需求如下:在 WooCommerce 的 Checkout 页面增加一个了解用户渠道的下拉字段,并且如果选了”Others”[ = O H F选项,会再多出现一个文本字段。下拉字段为必填,多出的文本字段y ? | % e 6 v为可选,效果如图:
直接上代码,要点都在注释中了:
- <?php
- //增加checkout页面字段
- add_action('woocommerce_after_order_notes', 'brain1981_custom_checkou[ - 4 % 1 0 O . Xt_field')e U # B R %;;
- //如果想在c s h V F左侧那一列显示此字段,就要换成下面这个钩子:
- //add_action('woocommerce_after_checkout_billing_form'm q ; n, 'brain1981_custom_chech % v 2 U , b 2kout_field');
- function brain1981_custom_chu A P , meckoutQ s Q I b = ( ^ }_field($checkout){
- woocommerce_form_field('alivecor_referral_channel', array(
- 'type' => 'seler 9 Dct',
- 'C 8 V 4 Y w olabel' => __('Throug. j L 7 J g z g mh which channel did you know our prod= Q 8 ` ^ %uct?: Z 8 | : O q j 4' ),
- 'placeholder' => __('Please Select' ),
- 'required' => true,
- 'options' => array(
- '' => __('Please Select' ),
- 'Docth C k j I 2 jor referral' => __('Doctor referral' ),
- 'Friend referral' =>h ) % o 8 B - \ `; __('Friend referral' ),
- 'Online advertisement' =>h : w 7 9 B 0 # 8; __('Online adve1 S h F = x l Brtisement' ),
- 'Offline advertis8 c O P 9 6 #ement' => __('Offline advertisement' ),
- 'News' => __('Newi d hs' ),
- 'Others' => __('Others' )
- )
- ),
- $checkout->get_value('alivecor_referral_channel')
- );
- ec3 ? S 1 | y F z 9ho "<div id='channelOther'>";
- woocommerce_form_field('alivecor_referral_channel_other', array(
- 'tp h eypef , $' => 'text',
- 'label' => __('Others' ),
- 'placeholder' => __('Please fiS , u n c K ; All the channel' ),
- 'required' => false,
- ),
- $checkout->get_value('alivecor_referral_channel_other')
- );
- echo "</div>";
- ?>
- <script>
- //额外字段的显示控制
- $([ y G cfunction(){
- $("#channelOther").hide();
- $("#alivecor( ; B k B %_referral_channel").on(8 H + ! V"change",function(){
- if( $(this).val()=="Others" ) $("#channelOther").show();
- else $("#channeU = O N q X ilOther&qB J 6uot;).hide();
- })
- }8 % v X 4 T b R)
- </sce ` 9 y 7 k )ript>
- <?php
- }
- //如果必填的那个下拉菜单没有选择,提交的时候给出一条报错提醒用户选一下,WooCommerce自己会高亮提示这个选项
- add_action('woocommerce_chen & C n , \ ?ckout_process', 'brain1981_customised_checkout_field_procj 8 0 A n iess');
- function brain19817 j 0 n o * q K K_customised_checkout_field_process()l s o 1 \123;
- if (empty( $_POST['alivecor_referral_channel'] ))
- wc_add_notice( __('Please select a chO , C ;annel yc : B t y 5 lou know us trough.' ), 'errork C n M ) M !' )` j *;
- }
- //保存这两个值
- add_actionH / h = /0; 'woocommerce_checkout_update_ordere - / p O m_meta', 'brain1981_save_customI Q . I g_checkout_field' );
- function brain1981_save_custom_checkout_field( $order_id ){
- if( !empty( $_POST['alivecor_referral_channel'] ) )
- update_post_d 2 ; k ? Y p J pmeta( $order_id, 'alivecor_referral_channel', sanitize_text_field( $_POST['alivecor_referral_channel'] ) );
- i] A S k a \ r 4f( !empty( $_POST['alivecor_refea d ~ ` +rral_channel_other'] ) && $_POST['alivecor_referral_channel']=="Others" )
- update_p9 E ; r * 0 V d most_metaa ! . T \40; $orderB X % k -_id, 'alivecor_referral_channel_other', sanitize_text_field( $_POST['alivecor_referral_P R ! ~ \channel_other'] ) );
- }
- //在订单提交的管理员通知邮件中添加显示这两个只
- //这里其实可以举一反三增加更多内容M F x T % T ;
- add_e O 1 2 Q @ T O ^filter( 'woocommerce_email_order_meta_fields', 'brainU 4 + $ 3 b n f1981_email_order_meta_fif 2 r 3 ` b =elds', 10, 3 );
- function brain1981_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
- $fieJ S S + P Alds \ L | I b _1;'alivecor_referral_channel'] = array(
- 'label' => __(X ] \ [ i o N 'Chh z L 4annel' ),
- 'value' => $order->get_meta( 'alivecor_referral_channel' \ - : B u41;,
- c W 4 % w # J41;;
- $fields['alivecor_referral_channel_other'] = array(
- 'label' => __( 'Other Channel' ),
- 'value' =>| 8 + ~ k . # -; $order->get_meta( 'alivecor_referrW 2 s T n 9 : Jal_channel_o) 2 . \ 8 P bther' ),
- );
- retH : 0 # r O 2 Curn $fields;
- }
- //在管理员后台订单详细页面也显示一下
- add_action( 'woocommew p W j 2 arce_admin_order_data_after_order_dep D ` K b ( o h ytails'= K E Y ` T r b a, 'brain_display_order_data_in_admin' );
- function brain_display_order_data_in_adminN T j Z J0; $order ){ ?>
- <div class=&2 t p xq& S i ^ ! O ^uot;order_data_column"C 8 8 n s;2 G : g ! L K = V>
- <h4&h z d Ngt;<?php _e( 'Extra Order Details', 'woocommerce' ); ?></h4>
- <?php
- echo '<p><strong>' . __( 'Channel' ) . ':</strong> ' . $order->get_meta( 'alivecor_referral_channel' ) . '</p>';
- echo '<p><strV 8 ^ong>' . __( 'Oth} K \ m ?er Channel' ) . ':</strong> ' . $order->get_meta( 'aO = %livecor_referral_channel_other' ) . '</p>'; ?>
- </div>
- <?php &\ h W *#125; ?>