在woocommerce如果我们需要用户下单时填写微信号方便我们客服联系,可以在主题的functions.php里增加以下代码:
add_action('woocommerce_after_order_notes', 'custom_checkout_field'); function custom_checkout_field($checkout) { echo '<div id="custom_checkout_field">'; woocommerce_form_field('weixin', array( 'type' => 'text', 'class' => array( 'my-field-class form-row-wide' ) , 'label' => __('微信号') , 'placeholder' => '' , ) , $checkout->get_value('weixin')); echo '</div>'; } //add_action('woocommerce_checkout_process', 'customised_checkout_field_process'); function customised_checkout_field_process() { // Show an error message if the field is not set. if (!$_POST['weixin']) wc_add_notice(__('请填写微信号') , 'error'); } add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta'); function custom_checkout_field_update_order_meta($order_id) { if (!empty($_POST['weixin'])) { update_post_meta($order_id, 'weixin',sanitize_text_field($_POST['weixin'])); } }
0 个评论