Comment ajouter le numéro de TVA et la raison sociale à Woocommerce avec un snippet

Voici un extrait que vous pouvez utiliser pour ajouter les champs numéro de TVA et raison sociale dans la zone de paiement de WooCommerce.

Programmazione WordPress
Programmazione WordPress

Ce code insère les champs obligatoires Numéro de TVA et Raison Sociale dans 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'] ) );
    }
}

Le code suivant insère les champs NON obligatoires Numéro de TVA et Raison Sociale dans 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

Se vuoi rimanere aggiornato su Comment ajouter le numéro de TVA et la raison sociale à Woocommerce avec un snippet iscriviti alla nostra newsletter settimanale

Soyez le premier à commenter

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée.


*