protected function loadEmailTemplate($order) { // Initialise $templateText = ''; $subject = ''; $loadLanguage = null; $isHTML = false; // Look for desired languages $jLang = JFactory::getLanguage(); if (JFactory::getUser($order->order->user_id)->id > 0) { $userLang = JFactory::getUser()->getParam('language', ''); } else { $userLang = $order->order->customer_language; } $languages = array($userLang, $jLang->getTag(), $jLang->getDefault(), 'en-GB', '*'); //load all templates $allTemplates = $this->getEmailTemplates($order); if (count($allTemplates)) { // Pass 1 - Give match scores to each template $preferredIndex = null; $preferredScore = 0; foreach ($allTemplates as $idx => $template) { // Get the language and level of this template $myLang = $template->language; // Make sure the language matches one of our desired languages, otherwise skip it $langPos = array_search($myLang, $languages); if ($langPos === false) { continue; } $langScore = 5 - $langPos; // Calculate the score $score = $langScore; if ($score > $preferredScore) { $loadLanguage = $myLang; $subject = $template->subject; $templateText = $template->body; $preferredScore = $score; $isHTML = true; } } } else { $config = JFactory::getConfig(); if (version_compare(JVERSION, '3.0', 'ge')) { $sitename = $config->get('sitename'); } else { $sitename = $config->getValue('config.sitename'); } $isHTML = true; $templateText = J2StoreOrdersHelper::_getHtmlFormatedOrder($order->order->id, $order->order->user_id); $subject = JText::sprintf('J2STORE_ORDER_USER_EMAIL_SUB', $order->order->billing_first_name . ' ' . $order->order->billing_last_name, $sitename); } return array($isHTML, $subject, $templateText, $loadLanguage); }
public static function sendUserEmail($user_id, $order_id, $payment_status, $order_status, $order_state_id) { $mainframe = JFactory::getApplication(); $config = JFactory::getConfig(); $j2params = JComponentHelper::getParams('com_j2store'); if (version_compare(JVERSION, '3.0', 'ge')) { $sitename = $config->get('sitename'); } else { $sitename = $config->getValue('config.sitename'); } //now get the order table's id based on order id $order = J2StoreOrdersHelper::_getOrderKey($order_id); //inventory //TODO::move this function to the plugin. require_once JPATH_ADMINISTRATOR . '/components/com_j2store/library/inventory.php'; require_once JPATH_ADMINISTRATOR . '/components/com_j2store/version.php'; J2StoreInventory::setInventory($order->id, $order_state_id); $mainframe->triggerEvent('onJ2StoreBeforeOrderNotification', array($order)); //now get the receipient $recipient = J2StoreOrdersHelper::_getRecipient($order->order_id); //check for email templates. If it is there, get the orderemail from there require_once JPATH_SITE . '/components/com_j2store/helpers/email.php'; $emailHelper = new J2StoreHelperEmail(); $orderObj = self::getOrder($order->id); if (count($emailHelper->getEmailTemplates($orderObj)) && J2STORE_PRO == 1) { $mailer = $emailHelper->getEmail($orderObj); } else { if ($user_id && empty($recipient->billing_first_name)) { $recipient->name = JFactory::getUser($user_id)->name; } else { $recipient->name = $recipient->billing_first_name . ' ' . $recipient->billing_last_name; } $body = J2StoreOrdersHelper::_getHtmlFormatedOrder($order->id, $user_id); $subject = JText::sprintf('J2STORE_ORDER_USER_EMAIL_SUB', $recipient->name, $sitename); $mailer = clone JFactory::getMailer(); $mailer->IsHTML(true); $mailer->CharSet = 'UTF-8'; $mailer->setSubject($subject); $mailer->setBody($body); } $admin_emails = $j2params->get('admin_email'); $admin_emails = explode(',', $admin_emails); if (version_compare(JVERSION, '3.0', 'ge')) { $mailfrom = $j2params->get('emails_defaultemail', $config->get('mailfrom')); $fromname = $j2params->get('emails_defaultname', $config->get('fromname')); } else { $mailfrom = $j2params->get('emails_defaultemail', $config->getValue('config.mailfrom')); $fromname = $j2params->get('emails_defaultname', $config->getValue('config.fromname')); } //send email if ($recipient) { $mailer->addRecipient($recipient->user_email); // $mailer->addBCC( $admin_emails ); $mailer->setSender(array($mailfrom, $fromname)); $mailer->send(); $mailer = null; } if ($admin_emails) { if (count($emailHelper->getEmailTemplates($orderObj)) && J2STORE_PRO == 1) { $mailer = $emailHelper->getEmail($orderObj); } else { $mailer = clone JFactory::getMailer(); $mailer->IsHTML(true); $mailer->CharSet = 'UTF-8'; $mailer->setSubject($subject); $mailer->setBody($body); } $mailer->addRecipient($admin_emails); $mailer->setSender(array($mailfrom, $fromname)); $mailer->send(); $mailer = null; } $mainframe->triggerEvent('onJ2StoreAfterOrderNotification', array($order)); return true; }