Viewing File: /home/fshjisk/atelierdemila/wp-content/themes/Avada/functions.php

<?php
/**
 * Extra files & functions are hooked here.
 *
 * Displays all of the head element and everything up until the "site-content" div.
 *
 * @package Avada
 * @subpackage Core
 * @since 1.0
 */

// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'Direct script access denied.' );
}

if ( ! defined( 'AVADA_VERSION' ) ) {
	define( 'AVADA_VERSION', '7.1.1' );
}

if ( ! defined( 'AVADA_MIN_PHP_VER_REQUIRED' ) ) {
	define( 'AVADA_MIN_PHP_VER_REQUIRED', '5.6' );
}

if ( ! defined( 'AVADA_MIN_WP_VER_REQUIRED' ) ) {
	define( 'AVADA_MIN_WP_VER_REQUIRED', '4.7' );
}

// Developer mode.
if ( ! defined( 'AVADA_DEV_MODE' ) ) {
	define( 'AVADA_DEV_MODE', false );
}

/**
 * Compatibility check.
 *
 * Check that the site meets the minimum requirements for the theme before proceeding.
 *
 * @since 6.0
 */
if ( version_compare( $GLOBALS['wp_version'], AVADA_MIN_WP_VER_REQUIRED, '<' ) || version_compare( PHP_VERSION, AVADA_MIN_PHP_VER_REQUIRED, '<' ) ) {
	require_once get_template_directory() . '/includes/bootstrap-compat.php';
	return;
}

/**
 * Bootstrap the theme.
 *
 * @since 6.0
 */
require_once get_template_directory() . '/includes/bootstrap.php';

/* Omit closing PHP tag to avoid "Headers already sent" issues. */

function wooc_extra_register_fields() {?>
       <p class="form-row form-row-wide">
       <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 esc_attr_e( $_POST['billing_phone'] ); ?>" />
       </p>
       <p class="form-row form-row-wide">
       <label for="reg_tva_number"><?php _e( 'Numéro de TVA', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="tva_number" id="reg_tva_number" value="<?php esc_attr_e( $_POST['tva_number'] ); ?>" required />
       </p>
       <p class="form-row form-row-first">
       <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'] ); ?>" />
       </p>
       <p class="form-row form-row-last">
       <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'] ); ?>" />
       </p>
       <div class="clear"></div>
       <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

/**
* register fields Validating.
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
	  if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
             $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!', 'woocommerce' ) );
      }
	  if ( isset( $_POST['tva_number'] ) && empty( $_POST['tva_number'] ) ) {
             $validation_errors->add( 'tva_number_error', __( '<strong>Error</strong>: Numéro de TVA is required!', 'woocommerce' ) );
      }
      if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
             $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
      }
      if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
             $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
      }
         return $validation_errors;
}

/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
	    if ( isset( $_POST['tva_number'] ) ) {
                 // Numéro de TVA input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'tva_number', sanitize_text_field( $_POST['tva_number'] ) );
			             // Tva field which is used in WooCommerce
             update_user_meta( $customer_id, 'tva_number', sanitize_text_field( $_POST['tva_number'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
      }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );


/**
 * COde for USER account TAX field creation.
 */

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

// For user Admin page
function extra_user_profile_fields( $user ) {
	$user_id = ( $user->ID ) ? $user->ID : get_current_user_id();
?>
    <h3><?php _e("User TVA Info", "blank"); ?></h3>

    <table class="form-table">
    <tr>
        <th <?php echo ( !is_admin() ) ? 'class="tva-frnt-hdng"' : ''; ?> ><label for="tva_number"><?php _e("TVA Number"); ?></label></th>
        <td>
            <input type="text" name="tva_number" id="tva_number" value="<?php echo esc_attr( get_the_author_meta( 'tva_number', $user_id ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Please enter your TVA number value."); ?></span>
        </td>
    </tr>
    </table>
<?php }

// To save Custom meta value
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {
    if ( isset( $_POST['tva_number'] ) ) { 
        update_user_meta( $user_id, 'tva_number', $_POST['tva_number'] );
    }
}

add_action( 'woocommerce_edit_account_form', 'extra_user_profile_fields', 10 );
add_action( 'woocommerce_save_account_details', 'save_extra_user_profile_fields', 10, 1 );

// For Woocommerce my account page
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_tva_fields' );

function custom_woocommerce_billing_tva_fields( $fields ){
	
    $fields['tva_number'] = array(
        'label' => __( 'TVA Number', 'woocommerce' ),
        'placeholder' => _x( 'Your TVA Number', 'placeholder', 'woocommerce' ),
        'required' => true,
        'clear' => false,
        'type' => 'text',
    );

    return $fields;
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'edit_woocommerce_checkout_page', 10, 1 );

function edit_woocommerce_checkout_page($order){
    global $post_id;
    $order = new WC_Order( $post_id );
	$user_id = get_post_meta($order->get_id(), '_customer_user', true );
	
    echo '<p><strong>'.__('TVA Number').':</strong> ' . get_user_meta($user_id, 'tva_number', true ) . '</p>';
}
Back to Directory File Manager