/*
Theme Name:           Flatsome
Theme URI:            http://flatsome.uxthemes.com
Author:               UX-Themes
Author URI:           https://uxthemes.com
Description:          Multi-Purpose Responsive WooCommerce Theme
Version:              3.18.3
Requires at least:    5.9
Requires PHP:         5.6
WC requires at least: 4.7
Text Domain:          flatsome
License:              https://themeforest.net/licenses
License URI:          https://themeforest.net/licenses
*/


/***************
All custom CSS should be added to Flatsome > Advanced > Custom CSS,
or in the style.css of a Child Theme.
***************/


// Voeg BTW-nummer veld toe aan het registratieformulier
add_action( 'woocommerce_register_form', 'btw_nummer_register_form' );

function btw_nummer_register_form() {
    ?>
    <p class="form-row form-row-wide">
        <label for="reg_btw_nummer"><?php _e( 'BTW Nummer', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="btw_nummer" id="reg_btw_nummer" value="<?php if ( ! empty( $_POST['btw_nummer'] ) ) echo esc_attr( wp_unslash( $_POST['btw_nummer'] ) ); ?>" />
    </p>
    <?php
}

// BTW-nummer veld valideren tijdens registratie
add_action( 'woocommerce_register_post', 'btw_nummer_register_post', 10, 3 );

function btw_nummer_register_post( $username, $email, $validation_errors ) {
    if ( isset( $_POST['btw_nummer'] ) && empty( $_POST['btw_nummer'] ) ) {
        $validation_errors->add( 'btw_nummer_error', __( 'BTW Nummer is een verplicht veld.', 'woocommerce' ) );
    }
}

// Sla BTW-nummer op tijdens registratie
add_action( 'woocommerce_created_customer', 'btw_nummer_created_customer' );

function btw_nummer_created_customer( $customer_id ) {
    if ( isset( $_POST['btw_nummer'] ) ) {
        update_user_meta( $customer_id, 'btw_nummer', sanitize_text_field( $_POST['btw_nummer'] ) );
    }
}

// Voeg BTW-nummer veld toe aan gebruikersprofielpagina
add_action( 'show_user_profile', 'btw_nummer_user_profile' );
add_action( 'edit_user_profile', 'btw_nummer_user_profile' );

function btw_nummer_user_profile( $user ) {
    ?>
    <h3><?php _e( 'BTW Informatie', 'woocommerce' ); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="btw_nummer"><?php _e( 'BTW Nummer', 'woocommerce' ); ?></label></th>
            <td>
                <input type="text" name="btw_nummer" id="btw_nummer" value="<?php echo esc_attr( get_user_meta( $user->ID, 'btw_nummer', true ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e( 'Voer het BTW Nummer van de gebruiker in.', 'woocommerce' ); ?></span>
            </td>
        </tr>
    </table>
    <?php
}

// Sla BTW-nummer op bij gebruikersprofiel
add_action( 'personal_options_update', 'btw_nummer_save_user_profile' );
add_action( 'edit_user_profile_update', 'btw_nummer_save_user_profile' );

function btw_nummer_save_user_profile( $user_id ) {
    if ( current_user_can( 'edit_user', $user_id ) ) {
        update_user_meta( $user_id, 'btw_nummer', sanitize_text_field( $_POST['btw_nummer'] ) );
    }
}

// BTW-nummer veld toevoegen aan het afrekenformulier
add_filter( 'woocommerce_checkout_fields', 'btw_nummer_checkout_field_autofill' );

function btw_nummer_checkout_field_autofill( $fields ) {
    $user_id = get_current_user_id();
    if ( $user_id ) {
        $btw_nummer = get_user_meta( $user_id, 'btw_nummer', true );
        $fields['billing']['billing_btw_nummer'] = array(
            'type'          => 'text',
            'label'         => __('BTW Nummer', 'woocommerce'),
            'placeholder'   => _x('BTW Nummer', 'placeholder', 'woocommerce'),
            'required'      => false,
            'class'         => array('form-row-wide'),
            'clear'         => true,
            'default'       => $btw_nummer,
        );
    }

    return $fields;
}

// BTW-nummer opslaan tijdens het afrekenen
add_action( 'woocommerce_checkout_update_user_meta', 'btw_nummer_checkout_field_update_user_meta' );

function btw_nummer_checkout_field_update_user_meta( $user_id, $posted ) {
    if ( ! empty( $posted['billing_btw_nummer'] ) ) {
        update_user_meta( $user_id, 'btw_nummer', sanitize_text_field( $posted['billing_btw_nummer'] ) );
    }
}


// Verwijder het BTW-nummer veld voordat de gegevens naar Mollie worden gestuurd
add_filter( 'woocommerce_payment_gateway_mollie_order_meta_data', 'remove_btw_nummer_from_mollie', 10, 2 );

function remove_btw_nummer_from_mollie( $order_meta_data, $order ) {
    if ( isset( $order_meta_data['billing_btw_nummer'] ) ) {
        unset( $order_meta_data['billing_btw_nummer'] );
    }
    return $order_meta_data;
}
