$find = array(
                    '{firstname}',
                    '{lastname}',
                    '{company}',
                    '{address_1}',
                    '{address_2}',
                    '{city}',
                    '{postcode}',
                    '{zone}',
                    '{zone_code}',
                    '{country}'
                );
    
                $replace = array(
                    'firstname' => $order_info['shipping_firstname'],
                    'lastname' => $order_info['shipping_lastname'],
                    'company' => $order_info['shipping_company'],
                    'address_1' => $order_info['shipping_address_1'],
                    'address_2' => $order_info['shipping_address_2'],
                    'city' => $order_info['shipping_city'],
                    'postcode' => $order_info['shipping_postcode'],
                    'zone' => $order_info['shipping_zone'],
                    'zone_code' => $order_info['shipping_zone_code'],
                    'country' => $order_info['shipping_country']
                );
    
                $data['shipping_address'] = str_replace(array("rn", "r", "n"), '<br />', preg_replace(array("/ss+/", "/rr+/", "/nn+/"), '<br />', trim(str_replace($find, $replace, $format))));
    
                $this->load->model('tool/upload');
    
                // Products

// Xtensions Best Checkout
                if($this->xtensions_checkout->isActive('xtensions_best_checkout',$this->config->get('config_store_id'))){
                    $formatted_address = $this->xtensions_checkout->getXtensionsModel($this->config->get('xtensions_custom_fields_model_path'))->getFormattedAddressForOrder($order_info);
                    $data['payment_address'] = $formatted_address['payment_address_formatted'];
                    $data['shipping_address'] = $formatted_address['shipping_address_formatted'];
                }
                // Xtensions Best Checkout
            
                $data['products'] = array();
    
                foreach ($order_product_query->rows as $product) {
                    $option_data = array();
    
                    $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");
    
                    foreach ($order_option_query->rows as $option) {
                        if ($option['type'] != 'file') {
                            $value = $option['value'];
                        } else {
                            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
    
                            if ($upload_info) {
                                $value = $upload_info['name'];
                            } else {
                                $value = '';
                            }
                        }
    
                        $option_data[] = array(
                            'name' => $option['name'],
                            'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
                        );
                    }
    
                    $data['products'][] = array(
                        'name' => $product['name'],
                        'model' => $product['model'],
                        'option' => $option_data,
                        'quantity' => $product['quantity'],
                        'price' => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
                        'total' => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
                    );
                }
    
                // Vouchers
                $data['vouchers'] = array();
    
                $order_voucher_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");
    
                foreach ($order_voucher_query->rows as $voucher) {
                    $data['vouchers'][] = array(
                        'description' => $voucher['description'],
                        'amount' => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
                    );
                }
    
                // Order Totals
                $data['totals'] = array();
                
                $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
    
                foreach ($order_total_query->rows as $total) {
                    $data['totals'][] = array(
                        'title' => $total['title'],
                        'text' => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
                    );
                }
    
                // Text Mail
                $text = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "nn";
                $text .= $language->get('text_new_order_id') . ' ' . $order_id . "n";
                $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "n";
                $text .= $language->get('text_new_order_status') . ' ' . $order_status . "nn";
    
                if ($comment && $notify) {
                    $text .= $language->get('text_new_instruction') . "nn";
                    $text .= $comment . "nn";
                }
    
                // Products
                $text .= $language->get('text_new_products') . "n";
    
                foreach ($order_product_query->rows as $product) {
                    $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "n";
    
                    $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $product['order_product_id'] . "'");
    
                    foreach ($order_option_query->rows as $option) {
                        if ($option['type'] != 'file') {
                            $value = $option['value'];
                        } else {
                            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);
    
                            if ($upload_info) {
                                $value = $upload_info['name'];
                            } else {
                                $value = '';
                            }
                        }
    
                        $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "n";
                    }
                }
    
                foreach ($order_voucher_query->rows as $voucher) {
                    $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
                }
    
                $text .= "n";
    
                $text .= $language->get('text_new_order_total') . "n";
    
                foreach ($order_total_query->rows as $total) {
                    $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "n";
                }
    
                $text .= "n";
    
                if ($order_info['customer_id']) {
                    $text .= $language->get('text_new_link') . "n";
                    $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "nn";
                }
    
                if ($download_status) {
                    $text .= $language->get('text_new_download') . "n";
                    $text .= $order_info['store_url'] . 'index.php?route=account/download' . "nn";
                }
    
                // Comment
                if ($order_info['comment']) {
                    $text .= $language->get('text_new_comment') . "nn";
                    $text .= $order_info['comment'] . "nn";
                }
    
                $text .= $language->get('text_new_footer') . "nn";
    
                $mail = new Mail();
                $mail->protocol = $this->config->get('config_mail_protocol');
                $mail->parameter = $this->config->get('config_mail_parameter');
                $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
                $mail->smtp_username = $this->config->get('config_mail_smtp_username');
                $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
                $mail->smtp_port = $this->config->get('config_mail_smtp_port');
                $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
    
                $mail->setTo($order_info['email']);
                $mail->setFrom($this->config->get('config_email'));
                $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
                $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
                $mail->setHtml($this->load->view('mail/order', $data));
                $mail->setText($text);
                
                //$mail->send();

                $this->load->model('tool/pro_email');

$email_params = array(
'type' => 'order.confirm',
'mail' => $mail,
'order_info' => $order_info,
'order_status_id' => $order_status_id,
'order_comment' => nl2br($comment),
);

$this->model_tool_pro_email->generate($email_params);
            
    
                // Admin Alert Mail
                if (in_array('order', (array)$this->config->get('config_mail_alert'))) {
                    $subject = sprintf($language->get('text_new_subject'), html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'), $order_id);
    
                    // HTML Mail
                    $data['text_greeting'] = $language->get('text_new_received');
    
                    if ($comment) {
                        if ($order_info['comment']) {
                            $data['comment'] = nl2br($comment) . '<br/><br/>' . $order_info['comment'];
                        } else {
                            $data['comment'] = nl2br($comment);
                        }
                    } else {
                        if ($order_info['comment']) {
                            $data['comment'] = $order_info['comment'];
                        } else {
                            $data['comment'] = '';
                        }
                    }
    
                    $data['text_download'] = '';
    
                    $data['text_footer'] = '';
    
                    $data['text_link'] = '';
                    $data['link'] = '';
                    $data['download'] = '';
    
                    // Text
                    $text = $language->get('text_new_received') . "nn";
                    $text .= $language->get('text_new_order_id') . ' ' . $order_id . "n";
                    $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "n";
                    $text .= $language->get('text_new_order_status') . ' ' . $order_status . "nn";
                    $text .= $language->get('text_new_products') . "n";
    
                    foreach ($order_product_query->rows as $product) {
                        $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "n";
    
                        $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $product['order_product_id'] . "'");
    
                        foreach ($order_option_query->rows as $option) {
                            if ($option['type'] != 'file') {
                                $value = $option['value'];
                            } else {
                                $value = utf8_substr($option['value'], 0, utf8_strrpos($option['value'], '.'));
                            }
    
                            $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "n";
                        }
                    }
    
                    foreach ($order_voucher_query->rows as $voucher) {
                        $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
                    }
    
                    $text .= "n";
    
                    $text .= $language->get('text_new_order_total') . "n";
    
                    foreach ($order_total_query->rows as $total) {
                        $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "n";
                    }
    
                    $text .= "n";
    
                    if ($order_info['comment']) {
                        $text .= $language->get('text_new_comment') . "nn";
                        $text .= $order_info['comment'] . "nn";
                    }
    
                    $mail = new Mail();
                    $mail->protocol = $this->config->get('config_mail_protocol');
                    $mail->parameter = $this->config->get('config_mail_parameter');
                    $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
                    $mail->smtp_username = $this->config->get('config_mail_smtp_username');
                    $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
                    $mail->smtp_port = $this->config->get('config_mail_smtp_port');
                    $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
    
                    $mail->setTo($this->config->get('config_email'));
                    $mail->setFrom($this->config->get('config_email'));
                    $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
                    $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
                    $mail->setHtml($this->load->view('mail/order', $data));
                    $mail->setText($text);
                    
                //$mail->send();

                $this->load->model('tool/pro_email');

$email_params = array(
'type' => 'admin.order.confirm',
'mail' => &$mail,
'order_info' => $order_info,
'order_status_id' => $order_status_id,
'order_comment' => nl2br($comment),
);

$this->model_tool_pro_email->generate($email_params);
            
    
                    // Send to additional alert emails
                    $emails = explode(',', $this->config->get('config_alert_email'));
    
                    foreach ($emails as $email) {
                        if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
                            $mail->setTo($email);
                            $mail->send();
                        }
                    }
                }
            }
    
            // If order status is not 0 then send update text email
            if ($order_info['order_status_id'] && $order_status_id && $notify) {
                $language = new Language($order_info['language_code']);
                $language->load($order_info['language_code']);
                $language->load('mail/order');
    
                $subject = sprintf($language->get('text_update_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);
    
                $message = $language->get('text_update_order') . ' ' . $order_id . "n";
                $message .= $language->get('text_update_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "nn";
    
                $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'");
    
                if ($order_status_query->num_rows) {
                    $message .= $language->get('text_update_order_status') . "nn";
                    $message .= $order_status_query->row['name'] . "nn";
                }
    
                if ($order_info['customer_id']) {
                    $message .= $language->get('text_update_link') . "n";
                    $message .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "nn";
                }
    
                if ($comment) {
                    $message .= $language->get('text_update_comment') . "nn";
                    $message .= strip_tags($comment) . "nn";
                }
    
                $message .= $language->get('text_update_footer');
    
                $mail = new Mail();
                $mail->protocol = $this->config->get('config_mail_protocol');
                $mail->parameter = $this->config->get('config_mail_parameter');
                $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
                $mail->smtp_username = $this->config->get('config_mail_smtp_username');
                $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
                $mail->smtp_port = $this->config->get('config_mail_smtp_port');
                $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
    
                $mail->setTo($order_info['email']);
                $mail->setFrom($this->config->get('config_email'));
                $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
                $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
                $mail->setText($message);
                
                        //$mail->send();

$this->load->model('tool/pro_email');

$email_params = array(
'type' => 'order.update',
'mail' => $mail,
'order_info' => $order_info,
'order_status_id' => (int)$order_status_id,
'order_status_name' => $order_status_query->num_rows ? $order_status_query->row['name'] : '',
'data' => array(
'order_status' => $order_status_query->num_rows ? $order_status_query->row['name'] : '',
'message' => nl2br($comment),
),
'conditions' => array(
'message' => $comment ? 1 : 0,
),
);

$this->model_tool_pro_email->generate($email_params);
            
            }
        }
    }
}