File: /var/www/school/wp-content/themes/school/functions.php
<?php
/**
 * GeneratePress child theme functions and definitions.
 *
 * Add your custom PHP in this file.
 * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
 */
// Register all menus in one function
function gp_child_register_menus()
{
    register_nav_menus(array(
        'primary' => __('Primary Menu', 'generatepress-child'),
        'top-menu' => __('Top Menu', 'generatepress-child'),
        'secondary-menu' => __('Secondary Menu', 'generatepress-child'),
    ));
}
add_action('after_setup_theme', 'gp_child_register_menus');
// Enqueue styles and scripts
function gp_child_enqueue_assets()
{
    // Parent GeneratePress CSS
    wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
    // Bootstrap RTL CSS
    wp_enqueue_style(
        'bootstrap-rtl',
        'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css',
        array(),
        '5.3.3'
    );
    // Bootstrap Icons
    wp_enqueue_style(
        'bootstrap-icons',
        'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css',
        array(),
        '1.11.3'
    );
    // Child theme CSS
    wp_enqueue_style(
        'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('parent-style', 'bootstrap-rtl')
    );
    // Enqueue jQuery explicitly
    wp_enqueue_script('jquery');
    // Bootstrap JS (bundle includes Popper)
    wp_enqueue_script(
        'bootstrap-js',
        'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js',
        array('jquery'),
        '5.3.3',
        true
    );
}
add_action('wp_enqueue_scripts', 'gp_child_enqueue_assets');
// Include Bootstrap Navwalker (only once, from child theme)
require_once get_stylesheet_directory() . '/inc/class-wp-bootstrap-navwalker.php';
add_role('instructor', 'مدرس', [
    'read' => true
    // Add more capabilities as needed.
]);
add_action('woocommerce_checkout_init', 'disable_billing');
function disable_billing($checkout)
{
    $checkout->checkout_fields['billing'] = array();
    return $checkout;
}
add_action('after_setup_theme', 'custom_disable_wc_gallery_features', 9999);
function custom_disable_wc_gallery_features()
{
    remove_theme_support('wc-product-gallery-zoom');
    remove_theme_support('wc-product-gallery-lightbox');
    remove_theme_support('wc-product-gallery-slider');
}
// Disable all WooCommerce shipping methods
add_action('woocommerce_cart_loaded_from_session', function() {
    WC()->shipping()->reset_shipping();
});
// Prevent shipping options from showing on checkout
add_filter('woocommerce_cart_needs_shipping', '__return_false');
add_filter('woocommerce_cart_needs_shipping_address', '__return_false');
add_filter('woocommerce_checkout_fields', 'custom_remove_checkout_fields');
function custom_remove_checkout_fields($fields) {
    // Remove billing fields
    $fields['billing'] = [];
    // Remove shipping fields
    $fields['shipping'] = [];
    return $fields;
}
function limit_purchase_quantity( $quantity, $product ) {
    return 1;
}
add_filter( 'woocommerce_quantity_input_max', 'limit_purchase_quantity', 10, 2 );
add_filter( 'woocommerce_quantity_input_min', 'limit_purchase_quantity', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', function() {
    return 'ثبتنام'; // Your Persian text for "Register for Course"
});
add_filter( 'woocommerce_order_button_text', function() {
    return 'تکمیل ثبتنام'; // Your Persian text for "Complete Registration"
});
function user_purchased_product_this_user($product_id, $user_id = null)
{
    if (!$user_id)
        $user_id = get_current_user_id();
    if (!$user_id)
        return false;
    $args = array(
        'numberposts' => -1,
        'post_type' => 'shop_order',
        'post_status' => array('wc-completed', 'wc-processing'), // or use WC_Order_Query
        'fields' => 'ids',
    );
    $orders = wc_get_orders(array(
        'customer' => $user_id,
        'status' => array('completed', 'processing'),
        'limit' => -1,
    ));
    foreach ($orders as $order_id) {
        $order = wc_get_order($order_id);
        foreach ($order->get_items() as $item) {
            if ($item->get_product_id() == $product_id) {
                return true;
            }
        }
    }
    return false;
}
add_action('my_custom_purchase_info_box', 'my_purchase_specific_box');
function my_purchase_specific_box()
{
    error_log('Hook my_purchase_specific_box fired');
    if (!is_user_logged_in())
        return;
    $product_id = get_the_ID();
    $user_id = get_current_user_id();  // Define user ID for logging
    error_log("User {$user_id} checking purchase on product {$product_id}");
    if (user_purchased_product_this_user($product_id, $user_id)) {
        ?>
        <div class="alert alert-success" role="alert">
            <h4 class="alert-heading">شرکت در دوره</h4>
            <p>شما در این دوره ثبتنام کردهاید.</p>
            
            <?php
            $registered_note = get_field('registered_note');
        if ($registered_note) {
            echo '<p>'. $registered_note .'</p>';
        }
            $field_group_keys = ['group_68f091b595682', 'group_68f0922a4e765', 'group_68f0709415997', 'group_68f0957766d04', 'group_68f095d29eaa7']; // Your multiple field group keys or IDs
            $fields = [];
            foreach ($field_group_keys as $group_key) {
                $fields_in_group = acf_get_fields($group_key);
                if (!$fields_in_group)
                    continue;
                foreach ($fields_in_group as $field) {
                    $value = get_field($field['name'], get_the_ID());
                    if (empty($value) && $value !== '0')
                        continue;
                    $fields[$field['name']] = $value;
                }
            }
            if ($fields) {
                ?>
                <ul class="list-group list-group-flush m-0 fd">
                    <?php
                    foreach ($fields as $field_key => $field_value) {
                        if ($field_key === 'registered_note')
                            continue;
                        $field_obj = get_field_object($field_key);
                        $label = $field_obj['label'] ?? ucfirst($field_key);
                        // Check if field is ACF datepicker type
                        if ($field_obj && $field_obj['type'] === 'date_picker' && !empty($field_value)) {
                            $value_html = "<span class='persianDate' data-timestamp='{$field_value}'></span>";
                        } elseif ($field_obj && $field_obj['type'] === 'date_time_picker' && !empty($field_value)) {
                            $value_html = "<span class='small persianTime' data-timestamp='{$field_value}'></span>";
                        } elseif ($field_obj && $field_obj['type'] === 'url' && !empty($field_value)) {
                            $value_html = "<a class='btn btn-nowruzgan btn-sm' target='_blank' href='" . esc_url($field_value) . "'>مشاهده</a>";
                        } elseif (is_array($field_value)) {
                            $value_html = implode(', ', array_map('esc_html', $field_value));
                        } else {
                            $value_html = esc_html($field_value);
                        }
                        echo "<li class='list-group-item d-flex justify-content-between align-items-center'>
                        <span>{$label}</span>
                        <strong>{$value_html}</strong>
                      </li>";
                    }
                    ?>
                </ul>
                <?php
            }
            ?>
        </div>
        <?php
    }
}
function get_product_category_url_by_id($category_id) {
    $term = get_term($category_id, 'product_cat');
    if ($term && !is_wp_error($term)) {
        return get_term_link($term);
    }
    return false; // or return empty string ''
}