在Dokan上添加自定义字段到现有表单(wordpress / woocommerce)

时间:2014-11-18 01:49:47

标签: php wordpress forms woocommerce dokan

我正在尝试在Dokan(woocommerce插件 - http://demo.wedevs.com/dokan/)卖家设置中添加自定义字段,以编辑woocommerce上用户地址的值。 Dokan在前端有一个表单供卖家编辑他的商店设置。我使用以下代码更改了我的主题functions.php:



 <?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' );
&#13;
&#13;
&#13;

表格显示确定,但它不会更新用户元。我无法做到的另一件事就是在表单输入字段上显示当前值。

我认为对于一个优秀的程序员来说这很容易。谁能帮我吗?非常感谢。

3 个答案:

答案 0 :(得分:1)

如果您想在Dokan设置中添加自定义字段ie Seller_url,请参阅下面的代码。

/*Extra field on the seller settings and show the value on the store banner -Dokan*/


// Add extra field in seller settings


add_filter( 'dokan_settings_after_banner', 'extra_fields', 10, 2);


function extra_fields( $current_user, $profile_info ){
$seller_url= isset( $profile_info['seller_url'] ) ? $profile_info['seller_url'] : '';
?>
 <div class="gregcustom dokan-form-group">
    <label class="dokan-w3 dokan-control-label" for="setting_address">
        <?php _e( 'Website', 'dokan' ); ?>
    </label>
    <div class="dokan-w5">
        <input type="text" class="dokan-form-control input-md valid" name="seller_url" id="reg_seller_url" value="<?php echo $seller_url; ?>" />
    </div>
</div>
<?php
}

下一步是使用以下代码保存设置。这不会覆盖现有设置。

    //save the field value


add_action( 'dokan_store_profile_saved', 'save_extra_fields', 15 );
function save_extra_fields( $store_id ) {
    if ( isset( $_POST['seller_url'] ) ) {
        $dokan_settings['seller_url'] = $_POST['seller_url'];
    }


    $existing_dokan_settings = get_user_meta( $store_id, 'dokan_profile_settings', true );
    $prev_dokan_settings     = ! empty( $existing_dokan_settings ) ? $existing_dokan_settings : array();
    $dokan_settings = array_merge( $prev_dokan_settings,$dokan_settings);
    update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}

答案 1 :(得分:0)

请尝试使用此代码:

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 );

希望它能解决你的问题。

答案 2 :(得分:0)

我遇到了同样的问题并找到了答案。我使用Sergiy Byelozyorov方法非常有帮助,但它部分工作,至少在我的情况下。

我发生的事情是只存储了新设置,并且在每次设置更新时删除了所有旧设置(换句话说,原始设置,而非自定义设置)。

这是因为在dokan中,do_action hook&#34; dokan_store_profile_saved&#34;只接受1个arg,即store_id。 没有传递dokan_settings,所以新函数

update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );

只更新了最新创建的$ dokan_settings数组,用空值覆盖所有其他$ dokan_settings args。

所以我不得不依赖变量$ store_id和函数get_user_meta()而且我确实喜欢这个(请考虑我需要另外一个与你需要的字段不同的字段):

function save_extra_concept_field( $store_id ) {
        if ( isset( $_POST['vendor_concept'] ) ) {
                $dokan_settings['vendor_concept'] = $_POST['vendor_concept'];
                //update_user_meta( $store_id, 'vendor_concept', sanitize_text_field( $_POST['vendor_concept'] ) ); 
        }
        $prev_dokan_settings = get_user_meta( $store_id, 'dokan_profile_settings', true );
        $dokan_settings = array_merge($prev_dokan_settings,$dokan_settings);
        update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}

希望这也有助于其他人。