How to add VAT number and company name to Woocommerce with a snippet

Here is a snippet that you can use to add the VAT and social name fields in the WooCommerce checkout area.

WordPress programmingWordPress programming

This code inserts iMandatory fields VAT and company namein 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 '
'; 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 '
'; } // 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 in Woocommerce.

function aggiungi_campi_partita_iva_e_ragione_sociale( $checkout ) {
    echo '
'; 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 '
'; } 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'] ) ); } }
Published in

If you want to stay updated onHow to add VAT and company name to WooCommerce with a snippet sign up for our weekly newsletter

Information about Anna Bruno 358 articles
Anna BrunoHe is a professional journalist, taking his first steps as a reporter for "The Gazzetta del Mezzogiorno”. Subsequently he collaborates with travel specialists (travel and food), including "Travel and Flavors"And"Travel People” and the major technology magazines. In 1998 he founded the communication and digital PR agency “FullPress Agency"” which has been publishing, since 2001, FullTravel.it, online travel magazine andVerdeGusto, food & wine magazine, of which he is managing director. Passionate about off-course, she often gets lost in new paths, waiting to be explored. She is the author of “Chat” (Jackson Libri, Milan, 2001), “Traveling with Internet” (Jackson Libri, Milan, 2001), “Virtual Communities” (Jackson Libri, Milan, 2002), “Digital Travel” (Dario Flaccovio Editore, Palermo, 2020), “Digital Food” (Dario Flaccovio Editore, Palermo, 2020) and the e-book “How to write effective press releases”. She is the delegate of the travel journalists of the Marche-Umbria-South of the GIST (Italian Tourist Press Group).Digital Travel & Food Specialist, speaker at events in the tourism and food sector and teacher in training courses.

Comment first

Leave a comment

The email address will not be published.


*