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.
I had the same problem and found this answer. I used Sergiy Byelozyorov method which was very helpful, but it was partially working, at least in my case.
What happened to me was that only the new settings were stored, and all the old settings (in other words the original ones, not customized) were deleted on each settings update.
This was because in dokan, the do_action hook "dokan_store_profile_saved" accepts only 1 arg, the store_id. There was no dokan_settings passed, so the new function
was updating only the latest $dokan_settings array created, overwriting all the other $dokan_settings args with empty values.
So I had to rely on the variable $store_id only and on function get_user_meta() and I did like this (please take into account that I needed another extra field different from the ones you needed):
Hope this helps someone else too.
If you want to add a custom field i.e. seller_url to Dokan setting, see code below.
Next step is to save setting by using below code. This will not overwrite the existing setting.
Please Try this code instead:
Hope it will solve your problems.