# Email Template Usage Guide ## How to Use Default Templates ### Quick Start The `DefaultTemplates` class provides production-ready email templates for all notification types. ```php use WooNooW\Email\DefaultTemplates; // Get all templates $templates = DefaultTemplates::get_all_templates(); // Get specific template $customerOrderPlaced = $templates['customer']['order_placed']; $staffOrderPlaced = $templates['staff']['order_placed']; // Get default subject $subject = DefaultTemplates::get_default_subject('customer', 'order_placed'); // Returns: "Your order #{order_number} has been received" ``` --- ## Template Structure ### Available Recipients - `customer` - Emails sent to customers - `staff` - Emails sent to store staff/admin ### Available Events **Customer Events:** - `order_placed` - When customer places an order - `order_confirmed` - When order is confirmed by staff - `order_shipped` - When order is shipped - `order_completed` - When order is delivered - `order_cancelled` - When order is cancelled - `payment_received` - When payment is successful - `payment_failed` - When payment fails - `customer_registered` - When customer creates account - `customer_vip_upgraded` - When customer becomes VIP **Staff Events:** - `order_placed` - New order notification - `order_confirmed` - Order confirmed notification - `order_shipped` - Order shipped notification - `order_completed` - Order completed notification - `order_cancelled` - Order cancelled notification - `payment_received` - Payment received notification - `payment_failed` - Payment failed notification --- ## Variable Replacement Templates use `{variable_name}` syntax. Replace these with actual data before sending: ```php $template = $templates['customer']['order_placed']; $subject = DefaultTemplates::get_default_subject('customer', 'order_placed'); // Replace variables $variables = [ 'customer_name' => $order->get_billing_first_name(), 'order_number' => $order->get_order_number(), 'order_date' => $order->get_date_created()->format('F j, Y'), 'order_total' => $order->get_formatted_order_total(), 'order_url' => $order->get_view_order_url(), 'payment_method' => $order->get_payment_method_title(), 'order_item_table' => $this->generate_order_items_table($order), 'support_email' => get_option('admin_email'), 'current_year' => date('Y'), 'site_name' => get_bloginfo('name'), ]; foreach ($variables as $key => $value) { $template = str_replace('{' . $key . '}', $value, $template); $subject = str_replace('{' . $key . '}', $value, $subject); } ``` --- ## Complete Variable Reference ### Order Variables ```php '{order_number}' // Order ID/number '{order_date}' // Order creation date '{order_total}' // Total amount with currency '{order_url}' // Link to view order '{order_item_table}' // HTML table of order items '{completion_date}' // Order completion date ``` ### Customer Variables ```php '{customer_name}' // Customer's full name '{customer_email}' // Customer's email address '{customer_phone}' // Customer's phone number ``` ### Payment Variables ```php '{payment_method}' // Payment method name '{payment_status}' // Payment status '{payment_date}' // Payment date '{transaction_id}' // Payment transaction ID '{payment_retry_url}' // URL to retry failed payment ``` ### Shipping Variables ```php '{tracking_number}' // Shipment tracking number '{tracking_url}' // Tracking URL '{shipping_carrier}' // Carrier name (e.g., "FedEx") '{shipping_address}' // Full shipping address '{billing_address}' // Full billing address ``` ### URL Variables ```php '{order_url}' // Order details page '{review_url}' // Leave review page '{shop_url}' // Shop homepage '{my_account_url}' // Customer account page '{vip_dashboard_url}' // VIP member dashboard ``` ### Store Variables ```php '{site_name}' // Store name '{store_url}' // Store homepage URL '{support_email}' // Support email address '{current_year}' // Current year (for copyright) ``` ### VIP Variables ```php '{vip_free_shipping_threshold}' // Free shipping threshold for VIP ``` --- ## Example: Sending Order Placed Email ```php use WooNooW\Email\DefaultTemplates; class OrderNotification { public function send_order_placed_email($order_id) { $order = wc_get_order($order_id); // Get template and subject $templates = DefaultTemplates::get_all_templates(); $body = $templates['customer']['order_placed']; $subject = DefaultTemplates::get_default_subject('customer', 'order_placed'); // Prepare variables $variables = [ 'customer_name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), 'order_number' => $order->get_order_number(), 'order_date' => $order->get_date_created()->format('F j, Y g:i A'), 'order_total' => $order->get_formatted_order_total(), 'order_url' => $order->get_view_order_url(), 'payment_method' => $order->get_payment_method_title(), 'order_item_table' => $this->generate_order_items_table($order), 'support_email' => get_option('woocommerce_email_from_address'), 'current_year' => date('Y'), 'site_name' => get_bloginfo('name'), ]; // Replace variables in body and subject foreach ($variables as $key => $value) { $body = str_replace('{' . $key . '}', $value, $body); $subject = str_replace('{' . $key . '}', $value, $subject); } // Convert markdown/card syntax to HTML $html = $this->parse_email_template($body); // Send email $headers = ['Content-Type: text/html; charset=UTF-8']; wp_mail( $order->get_billing_email(), $subject, $html, $headers ); } private function generate_order_items_table($order) { $html = '
| Product | '; $html .= 'Qty | '; $html .= 'Price | '; $html .= '
|---|---|---|
| '; $html .= '' . $item->get_name() . ''; $html .= ' | '; $html .= '' . $item->get_quantity() . ' | '; $html .= '' . wc_price($item->get_total()) . ' | '; $html .= '