Ajouter des champs aux formulaires WooCommerce

Dans cet article, je vais vous montrer comment ajouter des nouveaux champs aux formulaires WooCommerce

Admettons que nous souhaitons ajouter un champ Numéro de TVA pour les entreprises

Page Mon compte – Formulaire d’enregistrement – Ajout champ TVA

/** 1. Add VAT to My Account Register Form - WooCommerce **/

function add_vat_woo_account_registration() {
    <p class="vat form-row form-row-last" id="reg_billing_vat">
        <label for="reg_billing_vat"><?php _e( 'VAT number', 'Internationalization_OceanWP-child' ); ?> </label>
        <input type="text" class="input-text" name="billing_vat" value="<?php if ( !empty( $_POST['billing_vat'] ) ) esc_attr_e( $_POST['billing_vat'] ); ?>" />
 </p>
}
add_action( 'woocommerce_register_form_start', 'add_vat_woo_account_registration' );

/** 2. VALIDATE FIELDS **/
  
function validate_vat_fields( $errors, $username, $email ) {
    if ( isset( $_POST['billing_vat'] ) && empty( $_POST['billing_vat] ) ) {
        $errors->add( 'billing_vat_error', __( '<strong>Error</strong>: VAT is required!', 'woocommerce' ) );
    }
    return $errors;
}

add_filter( 'woocommerce_registration_errors', 'validate_vat_fields', 10, 3 );

/** 3. SAVE FIELDS **/
  
function save_fields( $customer_id ) {
    if ( isset( $_POST['billing_vat'] ) ) {
        update_user_meta( $customer_id, 'billing_vat', sanitize_text_field( $_POST['billing_vat'] ) );
        update_user_meta( $customer_id, 'billing_vat', sanitize_text_field($_POST['billing_vat']) );
    }
}

add_action( 'woocommerce_created_customer', 'save_fields' );

Page Mon compte/Page Paiement – Ajouter un champ à l’adresse de facturation – Ajout champ TVA

Page Mon compte – Ajout champ TVA

Page Paiement (checkout) – Ajout champ TVA

function add_woocommerce_billing_fields($billing_fields){
    $billing_fields2['billing_first_name'] = $billing_fields['billing_first_name'];
    $billing_fields2['billing_last_name'] = $billing_fields['billing_last_name'];
    $billing_fields2['billing_company'] = array(
        'type' => 'text',
         'label' =>  __('Company', 'woocommerce'),
         'class' => array('form-row-wide'),
         'required' => true,
         'clear' => true
        );
      $billing_fields2['billing_vat'] = array(
          'type' => 'text',
          'label' =>  __('VAT number', 'Internationalization_OceanWP-child' ),
          'class' => array('form-row-wide'),
          'required' => true,
          'clear' => true
        );
        
       $merged_billing_fields =  $billing_fields2 + $billing_fields;
    
       return $merged_billing_fields;
    }

add_filter('woocommerce_billing_fields' , 'add_woocommerce_billing_fields');
    

Page Mon Compte – Afficher un champ à l’adresse de facturation – Ajout champ TVA

function custom_my_account_my_address_formatted_address( $fields, $customer_id, $type ) {
    if ( $type == 'billing' ) {
        $fields['vat'] = get_user_meta( $customer_id, 'billing_vat', true );
    }
    return $fields;
    }

add_filter( 'woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3 );

Page Mon compte – Afficher un champ sur l’adresse de facturation – Ajout champ TVA

    function custom_my_account_my_address_formatted_address( $fields, $customer_id, $type ) {
    if ( $type == 'billing' ) {
        $fields['vat'] = get_user_meta( $customer_id, 'billing_vat', true );
    }
        return $fields;
    }

add_filter( 'woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3 );

Page Paiement (checkout) – Afficher un champ après avoir validé la paiement – Ajout champ TVA

function custom_add_vat_formatted_billing_address( $fields, $order ) {
    $values = get_post_custom( $order->get_id() );
    if(isset($values['_billing_vat'][0])) {
        $fields['vat'] = $values['_billing_vat'][0];
    }
    return $fields;	
}

add_filter( 'woocommerce_order_formatted_billing_address', 'custom_add_vat_formatted_billing_address', 10, 2 );

Back office WooCommerce – Modifier ma commande – Ajout champ TVA

function custom_admin_billing_fields( $fields ) {
    $fields['vat'] = array(
        'label' => __( 'VAT number', 'Internationalization_OceanWP-child' )  
     ); 
     return $fields;
}

add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );

Back office WooCommerce – Modifier ma commande (Mode édition) – Ajout champ TVA

function custom_found_customer_details( $customer_data ) {
    $customer_data['billing_vat'] = get_user_meta( $_POST['user_id'], 'billing_vat', true );
    return $customer_data;
    }

add_filter( 'woocommerce_found_customer_details', 'custom_found_customer_details' );

A propos d'Alexis

Dans le web depuis plus 5 ans, je me suis d'abord spécialisé en webmarketing pour ensuite m'investir dans la création de sites web vitrine, sites/boutiques e-commerce et applications web/plateforme web. J'ai ainsi la double compétence qui me permet d'être force de proposition pour la création de votre site marchand et la mise en place de votre stratégie de vente en ligne.

Laisser un commentaire