Ecco uno snippet che puoi utilizzare per aggiungere i campi Partita IVA e Ragione Sociale nell’area di checkout di WooCommerce.
Questo codice inserisce i campi obbligatori Partita Iva e ragione Sociale nel Woocommerce.
// Aggiungi i campi Partita IVA e Ragione Sociale nel checkout di WooCommerce
add_action('woocommerce_after_checkout_billing_form', 'aggiungi_campi_partita_iva_e_ragione_sociale');
function aggiungi_campi_partita_iva_e_ragione_sociale( $checkout ) {
echo '<div id="partita_iva_ragione_sociale">';
woocommerce_form_field( 'partita_iva', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Partita IVA', 'woocommerce'),
'placeholder' => __('Inserisci la tua Partita IVA', 'woocommerce'),
'required' => true,
), $checkout->get_value( 'partita_iva' ));
woocommerce_form_field( 'ragione_sociale', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Ragione Sociale', 'woocommerce'),
'placeholder' => __('Inserisci la tua Ragione Sociale', 'woocommerce'),
'required' => true,
), $checkout->get_value( 'ragione_sociale' ));
echo '</div>';
}
// Validazione dei campi Partita IVA e Ragione Sociale
add_action('woocommerce_checkout_process', 'valida_campi_partita_iva_e_ragione_sociale');
function valida_campi_partita_iva_e_ragione_sociale() {
if ( ! empty( $_POST['partita_iva'] ) && ! preg_match('/d{11}/', $_POST['partita_iva'] ) ) {
wc_add_notice( __( 'La Partita IVA non è valida.', 'woocommerce' ), 'error' );
}
if ( empty( $_POST['ragione_sociale'] ) ) {
wc_add_notice( __( 'La Ragione Sociale è obbligatoria.', 'woocommerce' ), 'error' );
}
}
// Salvataggio dei dati Partita IVA e Ragione Sociale nell'ordine
add_action('woocommerce_checkout_update_order_meta', 'salva_partita_iva_e_ragione_sociale');
function salva_partita_iva_e_ragione_sociale( $order_id ) {
if ( ! empty( $_POST['partita_iva'] ) ) {
update_post_meta( $order_id, 'Partita IVA', sanitize_text_field( $_POST['partita_iva'] ) );
}
if ( ! empty( $_POST['ragione_sociale'] ) ) {
update_post_meta( $order_id, 'Ragione Sociale', sanitize_text_field( $_POST['ragione_sociale'] ) );
}
}
Il codice che segue inserisce i campi NON obbligatori Partita Iva e ragione Sociale nel Woocommerce.
function aggiungi_campi_partita_iva_e_ragione_sociale( $checkout ) {
echo '<div id="partita_iva_ragione_sociale">';
woocommerce_form_field( 'partita_iva', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Partita IVA', 'woocommerce'),
'placeholder' => __('Inserisci la tua Partita IVA', 'woocommerce'),
'required' => false,
), $checkout->get_value( 'partita_iva' ));
woocommerce_form_field( 'ragione_sociale', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Ragione Sociale', 'woocommerce'),
'placeholder' => __('Inserisci la tua Ragione Sociale', 'woocommerce'),
'required' => false,
), $checkout->get_value( 'ragione_sociale' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_before_customer_details', 'aggiungi_campi_partita_iva_e_ragione_sociale' );
// Salvataggio dei dati Partita IVA e Ragione Sociale nell'ordine
add_action( 'woocommerce_checkout_update_order_meta', 'salva_partita_iva_e_ragione_sociale' );
function salva_partita_iva_e_ragione_sociale( $order_id ) {
if ( ! empty( $_POST['partita_iva'] ) ) {
update_post_meta( $order_id, 'Partita IVA', sanitize_text_field( $_POST['partita_iva'] ) );
}
if ( ! empty( $_POST['ragione_sociale'] ) ) {
update_post_meta( $order_id, 'Ragione Sociale', sanitize_text_field( $_POST['ragione_sociale'] ) );
}
} Pubblicato in WordPress

Commenta per primo