get_template_settings($event_id, $recipient_type); if (!$template_settings) { return null; } // Get recipient email $to = $this->get_recipient_email($recipient_type, $data); if (!$to) { return null; } // Get variables $variables = $this->get_variables($event_id, $data, $extra_data); // Replace variables in subject and content $subject = $this->replace_variables($template_settings['subject'], $variables); $content = $this->replace_variables($template_settings['body'], $variables); // Get HTML template design $design_template = $this->get_design_template($template_settings['design'] ?? 'modern'); // Render final HTML $html = $this->render_html($design_template, $content, $subject, $variables); return [ 'to' => $to, 'subject' => $subject, 'body' => $html, ]; } /** * Get template settings * * @param string $event_id * @param string $recipient_type * @return array|null */ private function get_template_settings($event_id, $recipient_type) { // Get saved template $template = TemplateProvider::get_template($event_id, 'email'); if (!$template) { return null; } // Get design template preference $settings = get_option('woonoow_notification_settings', []); $design = $settings['email_design_template'] ?? 'modern'; return [ 'subject' => $template['subject'] ?? '', 'body' => $template['body'] ?? '', 'design' => $design, ]; } /** * Get recipient email * * @param string $recipient_type * @param mixed $data * @return string|null */ private function get_recipient_email($recipient_type, $data) { if ($recipient_type === 'staff') { return get_option('admin_email'); } // Customer if ($data instanceof \WC_Order) { return $data->get_billing_email(); } if ($data instanceof \WC_Customer) { return $data->get_email(); } return null; } /** * Get variables for template * * @param string $event_id * @param mixed $data * @param array $extra_data * @return array */ private function get_variables($event_id, $data, $extra_data = []) { $variables = [ 'store_name' => get_bloginfo('name'), 'store_url' => home_url(), 'site_title' => get_bloginfo('name'), ]; // Order variables if ($data instanceof \WC_Order) { $variables = array_merge($variables, [ 'order_number' => $data->get_order_number(), 'order_id' => $data->get_id(), 'order_date' => $data->get_date_created()->date('F j, Y'), 'order_total' => $data->get_formatted_order_total(), 'order_subtotal' => wc_price($data->get_subtotal()), 'order_tax' => wc_price($data->get_total_tax()), 'order_shipping' => wc_price($data->get_shipping_total()), 'order_discount' => wc_price($data->get_discount_total()), 'order_status' => wc_get_order_status_name($data->get_status()), 'order_url' => $data->get_view_order_url(), 'payment_method' => $data->get_payment_method_title(), 'shipping_method' => $data->get_shipping_method(), 'customer_name' => $data->get_formatted_billing_full_name(), 'customer_first_name' => $data->get_billing_first_name(), 'customer_last_name' => $data->get_billing_last_name(), 'customer_email' => $data->get_billing_email(), 'customer_phone' => $data->get_billing_phone(), 'billing_address' => $data->get_formatted_billing_address(), 'shipping_address' => $data->get_formatted_shipping_address(), ]); // Order items $items_html = ''; foreach ($data->get_items() as $item) { $product = $item->get_product(); $items_html .= sprintf( '