HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/dev/dev-doccure-wp/wp-content/plugins/doccure/DURATION_FIELD_EXPLANATION.md
# Duration Field in Prescription Form - Explanation

## Overview
The **Duration** field in the prescription form is created using WordPress **taxonomy system** and displays predefined duration options that doctors can select from.

## How It Works

### 1. Taxonomy Registration
The `medicine_duration` taxonomy is registered in `wp-content/plugins/doccure/admin/post-types/prescription.php`:

```php
register_taxonomy('medicine_duration', array('prescription'), $duration_args);
```

### 2. Form Creation
In the prescription form templates, the Duration field is created using:

```php
<?php do_action( 'doccure_get_texnomy_select','medicine_duration','',esc_html__('Select medicine duration','doccure') ,'','medicine_duration');?>
```

This creates a dropdown with all available duration terms from the `medicine_duration` taxonomy.

### 3. Data Storage
When a prescription is saved, the duration is stored as a **term ID** (not the term name) in the medicine array:

```php
$medicine_duration_val = !empty($values['medicine_duration']) ? $values['medicine_duration'] : '';
```

### 4. Data Display (Fixed)
Previously, the View Prescription modal was displaying the raw term ID instead of the term name. This has been fixed by:

- Adding a helper function `doccure_get_term_name_from_id()`
- Converting term IDs to term names before display
- Using `get_term_by('id', $term_id, 'medicine_duration')` to get the actual term name

## Current Implementation

### Helper Function
```php
function doccure_get_term_name_from_id($term_id, $taxonomy) {
    if (empty($term_id) || empty($taxonomy)) {
        return '';
    }
    
    $term = get_term_by('id', $term_id, $taxonomy);
    return !empty($term) ? $term->name : $term_id;
}
```

### Usage in View Prescription Modal
```php
$duration = doccure_get_term_name_from_id($duration, 'medicine_duration');
```

## Taxonomy Structure

### Available Taxonomies for Medicine Fields:
1. **`medicine_types`** - Medicine types/categories
2. **`medicine_duration`** - Duration options (e.g., "7 days", "2 weeks", "1 month")
3. **`medicine_usage`** - Usage instructions (e.g., "Once daily", "Twice daily", "Before meals")

### Admin Management
- Duration terms can be managed in WordPress Admin under **Prescription > Medicine Duration**
- New duration options can be added by administrators
- Terms are hierarchical and can be organized

## Benefits of Taxonomy System

1. **Consistency**: All doctors use the same predefined options
2. **Standardization**: Ensures uniform duration formats
3. **Flexibility**: Admins can add/modify duration options
4. **Multilingual**: Terms can be translated
5. **Data Integrity**: Prevents typos and inconsistent entries

## Example Duration Terms
Common duration terms that might be available:
- 3 days
- 5 days
- 7 days
- 10 days
- 2 weeks
- 3 weeks
- 1 month
- 2 months
- 3 months
- As needed
- Until finished

## Troubleshooting

### If Duration Shows as Numbers:
- This means the term ID is being displayed instead of the term name
- Check if the `doccure_get_term_name_from_id()` function is being called
- Verify that the `medicine_duration` taxonomy exists and has terms

### If Duration is Empty:
- Check if the prescription has duration data saved
- Verify the taxonomy terms exist
- Check if the term ID is valid

## Related Files
- `wp-content/plugins/doccure/admin/post-types/prescription.php` - Taxonomy registration
- `wp-content/themes/doccure/directory/front-end/templates/doctors/dashboard-update-prescription.php` - Form template
- `wp-content/themes/doccure/directory/front-end/hooks.php` - `doccure_get_texnomy_select` function
- `wp-content/plugins/doccure/init.php` - View prescription modal implementation