/*
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.19.12
Requires at least:    6.4
Requires PHP:         7.4
WC requires at least: 8.3
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.
***************/









<?php
// Shortcode cho Form Đăng Nhập
function custom_login_form() {
    if (is_user_logged_in()) {
        return '<p>Bạn đã đăng nhập. <a href="' . wp_logout_url(home_url()) . '">Đăng xuất</a></p>';
    } else {
        ob_start();
        wp_login_form([
            'redirect' => home_url(),
            'label_username' => __('Tên đăng nhập hoặc Email'),
            'label_password' => __('Mật khẩu'),
            'label_log_in'   => __('Đăng nhập'),
        ]);
        return ob_get_clean();
    }
}
add_shortcode('custom_login', 'custom_login_form');

// Shortcode cho Form Đăng Ký
function custom_registration_form() {
    if (is_user_logged_in()) {
        return '<p>Bạn đã đăng nhập. <a href="' . wp_logout_url(home_url()) . '">Đăng xuất</a></p>';
    } else {
        ob_start();
        ?>
        <form action="<?php echo esc_url(site_url('wp-login.php?action=register', 'login_post')); ?>" method="post">
            <p>
                <label for="user_login">Tên đăng nhập</label>
                <input type="text" name="user_login" id="user_login" class="input" required>
            </p>
            <p>
                <label for="user_email">Email</label>
                <input type="email" name="user_email" id="user_email" class="input" required>
            </p>
            <?php do_action('register_form'); ?>
            <p>
                <input type="submit" name="wp-submit" id="wp-submit" value="Đăng ký">
            </p>
        </form>
        <?php
        return ob_get_clean();
    }
}
add_shortcode('custom_register', 'custom_registration_form');
?>





// Thêm trường Số điện thoại vào form đăng ký WooCommerce
add_action('woocommerce_register_form', 'add_phone_and_email_to_registration_form');
function add_phone_and_email_to_registration_form() {
    ?>
    <p class="form-row form-row-wide">
        <label for="reg_phone"><?php esc_html_e('Số điện thoại hoặc Email', 'woocommerce'); ?></label>
        <input type="text" class="input-text" name="phone_or_email" id="reg_phone" value="<?php if (!empty($_POST['phone_or_email'])) echo esc_attr($_POST['phone_or_email']); ?>" placeholder="Nhập số điện thoại hoặc Gmail của bạn"/>
    </p>
    <?php
}

// Lưu thông tin Số điện thoại hoặc Email
add_action('woocommerce_created_customer', 'save_phone_or_email_field');
function save_phone_or_email_field($customer_id) {
    if (isset($_POST['phone_or_email'])) {
        update_user_meta($customer_id, 'phone_or_email', sanitize_text_field($_POST['phone_or_email']));
    }
}












add_filter('woocommerce_order_get_formatted_billing_address', '__return_empty_array');
add_filter('woocommerce_email_order_meta_fields', function($fields) {
    unset($fields['billing']);
    return $fields;
});
