将自定义字段添加到Dokan上的现有表单(wordpress/woocoomerce) [英] Add custom fields to existing form on Dokan (wordpress/woocoomerce)
问题描述
我正在尝试在Dokan(woocommerce插件- http://demo.wedevs.com/dokan/)卖方设置,以在woocommerce上从用户地址编辑值. Dokan在前端有一个表格供卖家编辑他的商店设置.我使用以下代码更改了主题functions.php:
I am trying to add custom fields on Dokan (woocommerce plugin - http://demo.wedevs.com/dokan/) seller settings to edit values from the user address on woocommerce. Dokan has a form on frontend for sellers to edit his store settings. I changed my themes functions.php with this code:
<?php
function endereco() {
$user_id = get_current_user_id();
?> <div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'Cidade', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_first_name" id="reg_billing_first_name" value="<?php echo esc_attr_e( $_POST['billing_city'] ); ?>" />
</div> </div>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'Estado', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_first_name" id="reg_billing_first_name" value="<?php esc_attr_e( $_POST['billing_state'] ); ?>" />
</div>
</div>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'CEP', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_first_name" id="reg_billing_first_name" value="<?php esc_attr_e( $_POST['billing_postcode'] ); ?>" />
</div> </div>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'Endereço', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_first_name" id="reg_billing_first_name" value="<?php esc_attr_e( $_POST['billing_address_1'] ); ?>" />
</div>
</div>
<?php
}
add_filter( 'dokan_settings_after_banner', 'endereco');
/**
* Save the extra fields.
*
* @param int $customer_id Current customer ID.
*
* @return void
*/
function save_extra_endereco_fields( $customer_id ) {
if ( isset( $_POST['billing_city'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_postcode'] ) ) {
// WordPress default last name field.
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
}
if ( isset( $_POST['billing_state'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_address_1'] ) );
}
if ( isset( $_POST['billing_address_1'] ) ) {
// WooCommerce billing phone
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
}
}
add_action( 'dokan_store_profile_saved', 'save_extra_endereco_fields' );
表格显示正常,但是它不会更新用户元数据.没有错误我无法做的另一件事是在表单输入字段上显示当前值.
Form shows ok, but it just doesn't update user meta. Another thing i was unable to do without errors is to show the current value on the form imput field.
我认为对于一个好的程序员来说这很容易.谁能帮我吗?非常感谢.
i think this is quite easy for a good programmer. Can anyone help me out? Many thanks.
推荐答案
请改用以下代码:
function endereco( $current_user, $profile_info ) {
$billing_city = isset( $profile_info['billing_city'] ) ? $profile_info['billing_city'] : '';
$billing_postcode = isset( $profile_info['billing_postcode'] ) ? $profile_info['billing_postcode'] : '';
$billing_state = isset( $profile_info['billing_state'] ) ? $profile_info['billing_state'] : '';
$billing_address_1 = isset( $profile_info['billing_address_1'] ) ? $profile_info['billing_address_1'] : '';
?>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'Cidade', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_city" id="reg_billing_city" value="<?php echo $billing_city; ?>" />
</div>
</div>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'Estado', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_postcode" id="reg_billing_postcode" value="<?php echo $billing_postcode; ?>" />
</div>
</div>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'CEP', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_state" id="reg_billing_state" value="<?php echo $billing_postcode; ?>" />
</div>
</div>
<div class="gregcustom dokan-form-group">
<label class="dokan-w3 dokan-control-label" for="setting_address"><?php _e( 'Endereço', 'dokan' ); ?></label>
<div class="dokan-w5">
<input type="text" class="dokan-form-control input-md valid" name="billing_address_1" id="reg_billing_address_1" value="<?php echo $billing_address_1; ?>" />
</div>
</div>
<?php
}
add_filter( 'dokan_settings_after_banner', 'endereco', 10, 2);
/**
* Save the extra fields.
*
* @param int $customer_id Current customer ID.
*
* @return void
*/
function save_extra_endereco_fields( $store_id, $dokan_settings ) {
if ( isset( $_POST['billing_city'] ) ) {
$dokan_settings['billing_city'] = $_POST['billing_city'];
}
if ( isset( $_POST['billing_postcode'] ) ) {
$dokan_settings['billing_postcode'] = $_POST['billing_postcode'];
}
if ( isset( $_POST['billing_state'] ) ) {
$dokan_settings['billing_state'] = $_POST['billing_state'];
}
if ( isset( $_POST['billing_address_1'] ) ) {
$dokan_settings['billing_address_1'] = $_POST['billing_address_1'];
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
add_action( 'dokan_store_profile_saved', 'save_extra_endereco_fields', 10, 2 );
希望它将解决您的问题.
Hope it will solve your problems.
这篇关于将自定义字段添加到Dokan上的现有表单(wordpress/woocoomerce)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!