Salta la navigazione

Questo è il forum di assistenza tecnica di WPML, il plug-in multilingue di WordPress.

La sua lettura è permessa a tutti, ma la pubblicazione è riservata esclusivamente ai clienti di WPML. Il team di WPML risponde sul forum 6 giorni su 7, 22 ore su 24.

Questo ticket contiene 2 risposte, ha 1 voce.

Ultimo aggiornamento da paoloC-16 3 anni, 9 mesi fa.

Autore Messaggi
Febbraio 20, 2021 a 6:45 pm #8135955

paoloC-16

Tell us what you are trying to do?
Utilizzo B2Bking plugin con il quale genero dei campi di registrazione customizzati da impiegare nel form di registrazione. Ogni campo è un custom post type che poi traduco con wpml. Nel frontend in pratica in outuput per ciascun campo quindi due campi uno per la lingua predefinita ed uno per la lingua inglese, uno dei dei due viene nascosto automaticamente e correttamente in base alla lingua.

Quando si invia il form deve essere salvato il valore immesso come valore per la metakey

b2bking_custom_field_'.$field->ID

Putroppo durante il salvataggio anzichè prendere l'id del campo per la lingua corrente viene usato l'id della lingua inglese che è vuoto.

Ho avvisato il supporto del plugin ma non mi rispondo. Vorrei un aiuto per modificare il codice correttamente.

la funzione che si occupa di salvare i campi è la seguente.

// Save Custom Registration Fields
function b2bking_save_custom_registration_fields($user_id){

if (get_user_meta($user_id, 'b2bking_registration_data_saved', true) === 'yes'){
// function has already run
return;
} else {
update_user_meta($user_id,'b2bking_registration_data_saved', 'yes');
}

// not relevant if this is a dokan seller
if (isset($_POST['role'])){
if (sanitize_text_field($_POST['role']) === 'seller'){
return;
}
}

$custom_fields_string = '';

// get all enabled custom fields
$custom_fields = get_posts([
'post_type' => 'b2bking_custom_field',
'post_status' => 'publish',
'numberposts' => -1,
'meta_key' => 'b2bking_custom_field_sort_number',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
'relation' => 'AND',
array(
'key' => 'b2bking_custom_field_status',
'value' => 1
),

)
]);
// loop through fields
foreach ($custom_fields as $field){

// if field is checkbox, check checkbox options and save them
$field_type = get_post_meta($field->ID, 'b2bking_custom_field_field_type', true);

if ($field_type === 'checkbox'){

// add field to fields string
$custom_fields_string .= $field->ID.',';

$select_options = get_post_meta($field->ID, 'b2bking_custom_field_user_choices', true);
$select_options = explode(',', $select_options);
$i = 1;
foreach ($select_options as $option){

// get field and check if set
$field_value = sanitize_text_field(filter_input(INPUT_POST, 'b2bking_custom_field_'.$field->ID.'_option_'.$i));
if (intval($field_value) === 1){
update_user_meta( $user_id, 'b2bking_custom_field_'.$field->ID.'_option_'.$i, $option);
// if have a selected value, give a value of 1 to the field, so we know to display it in the backend
update_user_meta( $user_id, 'b2bking_custom_field_'.$field->ID, 1);
}
$i++;
}

}

// get field and check if set
$field_value = sanitize_text_field(filter_input(INPUT_POST, 'b2bking_custom_field_'.$field->ID));
if ($field_value !== NULL && $field_type !== 'checkbox'){
update_user_meta( $user_id, 'b2bking_custom_field_'.$field->ID, $field_value);

// Also set related field data as user meta.
// Relevant fields: field type, label and user_choices

// add field to fields string
$custom_fields_string .= $field->ID.',';

$field_type = get_post_meta($field->ID, 'b2bking_custom_field_field_type', true);
$field_label = get_post_meta($field->ID, 'b2bking_custom_field_field_label', true);
if ($field_type === 'file' ){
if ( ! empty( $_FILES['b2bking_custom_field_'.$field->ID]['name'] ) ){
// has already been checked for errors (type/size) in b2bking_custom_registration_fields_check_errors function
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );

// Upload the file
$attachment_id = media_handle_upload( 'b2bking_custom_field_'.$field->ID, 0 );
// Set attachment author as the user who uploaded it
$attachment_post = array(
'ID' => $attachment_id,
'post_author' => $user_id
);
wp_update_post( $attachment_post );

// set attachment id as user meta
update_user_meta( $user_id, 'b2bking_custom_field_'.$field->ID, $attachment_id );
}
}

// if field has billing connection, update billing user meta
$billing_connection = get_post_meta($field->ID, 'b2bking_custom_field_billing_connection', true);
if ($billing_connection !== 'none'){
// special situation for countrystate combined field
if($billing_connection === 'billing_countrystate'){
update_user_meta ($user_id, 'billing_country', $field_value);
// get state as well
$state_value = sanitize_text_field(filter_input(INPUT_POST, 'billing_state'));
update_user_meta ($user_id, 'billing_state', $state_value);
} else {
// field value name is identical to billing user meta field name
if ($billing_connection !== 'custom_mapping'){
update_user_meta ($user_id, $billing_connection, $field_value);
} else {
update_user_meta ($user_id, sanitize_text_field(get_post_meta($field->ID, 'b2bking_custom_field_mapping', true)), $field_value);
}
// if field is first name or last name, add it to account details (Sync)
if ($billing_connection === 'billing_first_name'){
update_user_meta( $user_id, 'first_name', $field_value );
} else if ($billing_connection === 'billing_last_name'){
update_user_meta( $user_id, 'last_name', $field_value );
}
}
}
}
}

// set string of custom field ids as meta
if ($custom_fields_string !== ''){
update_user_meta( $user_id, 'b2bking_custom_fields_string', $custom_fields_string);
}

// if user role dropdown enabled, also set user registration role as meta
if (isset($_POST['b2bking_registration_roles_dropdown'])){
$user_role = sanitize_text_field(filter_input(INPUT_POST, 'b2bking_registration_roles_dropdown'));
if ($user_role !== NULL){
update_user_meta( $user_id, 'b2bking_registration_role', $user_role);
}
}

// if VIES VAT Validation is Enabled AND VAT field is not empty, set vies-validated vat meta
if (isset($_POST['b2bking_vat_number_registration_field_number'])){
$vat_number_inputted = sanitize_text_field($_POST['b2bking_custom_field_'.$_POST['b2bking_vat_number_registration_field_number']]);
$vat_number_inputted = strtoupper(str_replace(array('.', ' '), '', $vat_number_inputted));
if (!(empty($vat_number_inputted))){
// check if VIES Validation is enabled in settings
$vat_field_vies_validation_setting = get_post_meta($_POST['b2bking_vat_number_registration_field_number'], 'b2bking_custom_field_VAT_VIES_validation', true);
// proceed only if VIES validation is enabled
if (intval($vat_field_vies_validation_setting) === 1){
update_user_meta($user_id, 'b2bking_user_vat_status', 'validated_vat');
}

// if cookie, set validate vat also
if (isset($_COOKIE['b2bking_validated_vat_status'])){
update_user_meta($user_id, 'b2bking_user_vat_status', sanitize_text_field($_COOKIE['b2bking_validated_vat_status']));
}
}
}

// if settings require approval on all users OR chosen user role requires approval
if (intval(get_option('b2bking_approval_required_all_users_setting', 0)) === 1){
update_user_meta( $user_id, 'b2bking_account_approved', 'no');

} else if (isset($_POST['b2bking_registration_roles_dropdown'])){
$user_role = sanitize_text_field(filter_input(INPUT_POST, 'b2bking_registration_roles_dropdown'));
$user_role_id = explode('_', $user_role)[1];
$user_role_approval = get_post_meta($user_role_id, 'b2bking_custom_role_approval', true);
$user_role_automatic_customer_group = get_post_meta($user_role_id, 'b2bking_custom_role_automatic_approval_group', true);

if ($user_role_approval === 'manual'){
update_user_meta( $user_id, 'b2bking_account_approved', 'no');
// check if there is a setting to automatically send the user to a particular customer group
if ($user_role_automatic_customer_group !== 'none' && $user_role_automatic_customer_group !== NULL && $user_role_automatic_customer_group !== ''){
update_user_meta($user_id,'b2bking_default_approval_manual', $user_role_automatic_customer_group);
}
} else if ($user_role_approval === 'automatic'){
// check if there is a setting to automatically send the user to a particular customer group
if ($user_role_automatic_customer_group !== 'none' && $user_role_automatic_customer_group !== NULL && $user_role_automatic_customer_group !== ''){
$group_id = explode('_',$user_role_automatic_customer_group)[1];
update_user_meta( $user_id, 'b2bking_customergroup', sanitize_text_field($group_id));
$user_obj = new WP_User($user_id);
$user_obj->add_role('b2bking_role_'.$group_id);
}
}
}

// if customer is being approved automatically, and group is other than none, set customer as B2B
$user_role = sanitize_text_field(filter_input(INPUT_POST, 'b2bking_registration_roles_dropdown'));
$user_role_id = explode('_', $user_role)[1];
$user_role_approval = get_post_meta($user_role_id, 'b2bking_custom_role_approval', true);
if ($user_role_approval === 'automatic'){
if ($user_role_automatic_customer_group !== 'none' && metadata_exists('post', $user_role_id, 'b2bking_custom_role_automatic_approval_group')){
update_user_meta($user_id, 'b2bking_b2buser', 'yes');
} else {
// user must be b2c, add b2c role
$user_obj = new WP_User($user_id);
$user_obj->add_role('b2bking_role_b2cuser');
}
}

$user_is_b2b = get_user_meta($user_id,'b2bking_b2buser', true);

if (!isset($_POST['b2bking_registration_roles_dropdown']) && $user_is_b2b !== 'yes'){
// must be a default b2c registration, add b2c role
$user_obj = new WP_User($user_id);
$user_obj->add_role('b2bking_role_b2cuser');
}

}

Febbraio 20, 2021 a 10:26 pm #8136293

paoloC-16

il codice chiave è questo:
$custom_fields = get_posts([
'post_type' => 'b2bking_custom_field',
'post_status' => 'publish',
'numberposts' => -1,
'meta_key' => 'b2bking_custom_field_sort_number',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
'relation' => 'AND',
array(
'key' => 'b2bking_custom_field_status',
'value' => 1
),
),
]);

che prende tutti i campi anziche solo quelli per la lingua corrente. Ora ho provato ad aggiungere 'suppress_filters' => false ma non va quindi molto probabilmente a questo punto dell'esecuzione la il codice non è in grado di capire quale sia la lingua corrente.