0

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

フォームは正常に表示されますが、ユーザー メタが更新されません。エラーなしではできなかったもう 1 つのことは、フォームの入力フィールドに現在の値を表示することです。

これは、優れたプログラマーにとって非常に簡単だと思います。誰でも私を助けることができますか?どうもありがとう。

4

3 に答える 3

0

私は同じ問題を抱えていて、この答えを見つけました。Sergiy Byelozyorov メソッドを使用しましたが、これは非常に役に立ちましたが、少なくとも私の場合は部分的に機能していました。

私に起こったことは、新しい設定のみが保存され、すべての古い設定 (つまり、カスタマイズされていない元の設定) が設定の更新ごとに削除されたことです。

これは、dokan では、do_action フック "dokan_store_profile_saved" が store_id という 1 つの引数しか受け入れないためです。dokan_settings が渡されていなかったので、新しい関数

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

作成された最新の $dokan_settings 配列のみを更新し、他のすべての $dokan_settings 引数を空の値で上書きしていました。

したがって、変数 $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 );
}

これが他の誰かにも役立つことを願っています。

于 2016-02-16T13:42:55.963 に答える
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 );

それがあなたの問題を解決することを願っています。

于 2014-11-20T09:48:59.407 に答える