ACF Repeater Email Templates
August 7, 2025 · AI Conversations
—
## ✅ Step-by-Step Guide
### 1. **Create the ACF Repeater Field Group**
Go to **ACF → Field Groups** and click **Add New**.
#### Set Location Rules:
– Show this field group if: **Options Page** is equal to **Email Templates** (we’ll create this options page next).
#### Add a **Repeater Field**:
– **Field Label:** Email Templates
– **Field Name:** `email_templates`
– **Field Type:** Repeater
Inside the repeater, add fields like:
| Label | Name | Type |
|——————–|——————–|—————|
| Template Name | `template_name` | Text |
| Email Subject | `email_subject` | Text |
| Email Body | `email_body` | WYSIWYG Editor |
Save the field group.
—
### 2. **Register the ACF Options Page**
Add this to your `functions.php`:
if (function_exists('acf_add_options_page')) {
acf_add_options_page([
'page_title' => 'Email Templates',
'menu_title' => 'Email Templates',
'menu_slug' => 'email-templates',
'capability' => 'edit_posts',
'redirect' => false
]);
}
—
### 3. **Use Email Templates in Code**
To fetch the templates dynamically:
$email_templates = get_field('email_templates', 'option');
if ($email_templates) {
foreach ($email_templates as $template) {
echo '<h3>' . esc_html($template['template_name']) . '</h3>';
echo '<p><strong>Subject:</strong> ' . esc_html($template['email_subject']) . '</p>';
echo '<div>' . wp_kses_post($template['email_body']) . '</div>';
}
}
—
### 4. **Optional: Fetch by Template Name**
Create a helper function to get a template by name:
function get_email_template_by_name($name) {
$templates = get_field('email_templates', 'option');
foreach ($templates as $template) {
if ($template['template_name'] === $name) {
return $template;
}
}
return null;
}
Usage:
$template = get_email_template_by_name('welcome_email');
if ($template) {
$subject = $template['email_subject'];
$body = $template['email_body'];
// use in wp_mail() or other logic
}
—
Let me know if you want to:
– Attach files to templates
– Use shortcodes/placeholders inside templates
– Add a preview/test email button
I can help you extend this!
—
## 🔧 Goal:
Let admins select one of the **email templates (from ACF Repeater on Options Page)** **when editing a product** in WooCommerce.
—
## ✅ Step-by-Step Integration
### 1. **Add ACF Select Field to Product Page**
Go to **ACF → Field Groups** and click **Add New Field Group**.
#### Settings:
– **Location Rules**:
– Post Type **is equal to** **Product**
#### Add a New Field:
– **Label**: Email Template
– **Name**: `product_email_template`
– **Type**: Select
– **Choices**: Leave **blank** — we’ll populate it dynamically.
– **Return Format**: `Value` or `Label` (recommended: **Value**)
#### Under “Advanced” → set:
– **Allow Null**: Yes (optional)
– **UI**: Yes (makes it a nice dropdown)
Save the field group.
—
### 2. **Populate Select Field Choices Dynamically (via PHP)**
Add this to your `functions.php` or custom plugin:
add_filter('acf/load_field/name=product_email_template', 'populate_email_template_choices');
function populate_email_template_choices($field) {
$templates = get_field('email_templates', 'option');
$choices = [];
if (!empty($templates)) {
foreach ($templates as $template) {
$name = $template['template_name'] ?? '';
if ($name) {
$choices[$name] = $name; // key and value both as template name
}
}
}
$field['choices'] = $choices;
return $field;
}
> This will dynamically fill the select dropdown on the **WooCommerce product edit page** with email template names from your repeater field.
—
### 3. **Usage in Code (Front or Backend)**
To fetch the selected email template for a product:
$template_name = get_field('product_email_template', get_the_ID());
$template = get_email_template_by_name($template_name);
if ($template) {
$subject = $template['email_subject'];
$body = $template['email_body'];
// You can use this in emails or custom logic
}
Make sure `get_email_template_by_name()` is defined as shown in the earlier message.
—
## 🧪 Optional Ideas
– ✅ Trigger this email on product purchase
– ✉️ Send to customer/admin after checkout
– 👀 Add preview feature or show template content in admin
Would you like help with triggering the selected template **after a WooCommerce order is placed** or showing a **preview of the selected template** on the product page?