Open vyskoczilova opened 4 years ago
Upravil jsem si registrační formulář, protože jsme velkoobchod, tak, aby se zadávalo i IČ a DIČ. Hodně by se mi hodila možnost ověřovat tyto údaje už při registraci/vytváření účtu. Plánujete to?
@Aramon Dobrý den, nechcete úpravu registračního formuláře pushnout jako změnu, že bychom pak přidali ověření při registraci? Předpokládám, že asi používáte standardní WooCommerce registrační formulář, že?
Používám tuto úpravu, vyplnění IČ/DIČ není povinné. Určitě by bylo zajímavé mít možnost zaškrtnout Registrace na firmu a pak by IČ mělo být povinné. A úplně ideální možnost nastavit "firemním" zákazníkům nějakou jinou roli než zákazník:
// úprava registračního formuláře
function wooc_extra_register_fields() {
?>
<strong>Vytvoření účtu neopravňuje k nákupu. Registrace nového účtu podléhá ručnímu schválení, o čemž budete informováni po kontrole údajů emailem.<br />Velkobchod VeloStore není určen pro soukromé osoby.</strong><br />Položky označené * jsou povinné.<br /><br />
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
<div class="clear"></div>
<label for="reg_billing_company"><?php _e( 'Název firmy, skupiny, spolku, klubu, nadace...', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
<label for="reg_billing_ic"><?php _e( 'IČ', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_ic" id="reg_billing_ic" value="<?php if ( ! empty( $_POST['billing_ic'] ) ) esc_attr_e( $_POST['billing_ic'] ); ?>" />
<label for="reg_billing_dic"><?php _e( 'DIČ', 'woocommerce' ); ?></label>
<input type="text" class="input-text" name="billing_dic" id="reg_billing_dic" value="<?php if ( ! empty( $_POST['billing_dic'] ) ) esc_attr_e( $_POST['billing_dic'] ); ?>" />
<div class="clear"></div>
<label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
<label for="reg_billing_address_1"><?php _e( 'Address', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
<label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
<label for="reg_billing_postcode"><?php _e( 'ZIP', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
// kontrola údajů
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( 'Jméno je povinná položka!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$validation_errors->add( 'billing_last_name_error', __( 'Příjmení je povinná položka!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
$validation_errors->add( 'billing_phone_error', __( 'Telefon je povinná položka!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
$validation_errors->add( 'billing_address_1_error', __( 'Adresa je povinná položka!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
$validation_errors->add( 'billing_city_error', __( 'Město je povinná položka!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {
$validation_errors->add( 'billing_postcode_error', __( 'PSČ je povinná položka!.', 'woocommerce' ) );
}
if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
$validation_errors->add( 'billing_company_error', __( 'Název firmy, spolku atd je povinná položka!.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
// uložení údajů zákazníka do databáze
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
// WordPress default first name field.
update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
// WooCommerce billing first name.
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
// aktualizujeme Jméno
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
// aktualizujeme Příjmení
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
}
if ( isset( $_POST['billing_phone'] ) ) {
// aktualizujeme Telefon
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
if ( isset( $_POST['billing_address_1'] ) ) {
// aktualizujeme Adresu/ulici
update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
}
if ( isset( $_POST['billing_city'] ) ) {
// aktualizujeme Město
update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
}
if ( isset( $_POST['billing_postcode'] ) ) {
// aktualizujeme PSČ
update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
}
if ( isset( $_POST['billing_company'] ) ) {
// aktualizujeme název firmy
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['billing_ic'] ) ) {
// aktualizujeme IČ
update_user_meta( $customer_id, 'billing_ic', sanitize_text_field( $_POST['billing_ic'] ) );
}
if ( isset( $_POST['billing_dic'] ) ) {
// aktualizujeme DIČ
update_user_meta( $customer_id, 'billing_dic', sanitize_text_field( $_POST['billing_dic'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
@Aramon Díky! Bude to potřeba trochu uhladit, ale každopádně, pokud chcete validovat data, tak můžete:
DIČ (logika podrobně zde - asi chcete kontrolovat ještě zemi)
$validator = new Ibericode\Vat\Validator();
if ( ! $validator->validateVatNumberFormat( $dic )) {
// vyhodit hlášku
}
A stejně tak můžete otestovat IČO vůči Aresu woolab_icdic_ares( $ico );
pokud je české
https://wordpress.org/support/topic/overovani-ico-v-ares-muj-ucet/#post-11970952