Jump to content

Edit History

Pandal

Pandal

<?php

class [YourModuleName]ConfirmationEmailModuleFrontController extends ModuleFrontController
{
  public function initContent()
  { 
    if(!Tools::getValue("id_order")){
      die('Missing get parameter id_order');
    }

    self::sendOrderConfirmationEmail(intval(Tools::getValue("id_order")));
  }

  private static function sendOrderConfirmationEmail($id_order)
  {

    $order = new Order($id_order);
    $context = Context::getContext();
    $invoice = new Address($order->id_address_invoice);
    $delivery = new Address($order->id_address_delivery);
    $delivery_state = $delivery->id_state ? new State((int) $delivery->id_state) : false;
    $invoice_state = $invoice->id_state ? new State((int) $invoice->id_state) : false;
    $carrier = $order->id_carrier ? new Carrier($order->id_carrier) : false;
    $customer = $order->getCustomer();
    $orderLanguage = new Language((int) $order->id_lang);
    $currency = new Currency($order->id_currency);
    
    $product_var_tpl_list = array();
    
    foreach ($order->getProducts() as $product) {

      $vars = array();
      $vars['unit_price'] = Tools::displayPrice($product['unit_price_tax_incl'], $currency, false);
      $vars['price'] = Tools::displayPrice($product['total_price_tax_incl'], $currency, false);
      $vars['reference'] = $product['reference'];
      $vars['name'] = $product['product_name'];
      $vars['quantity'] = $product['product_quantity'];
      $vars['customization'] = array();

      array_push($product_var_tpl_list, $vars);
    }

    $cart = Cart::getCartByOrderId($order->id);

    $cart_rules = $cart->getCartRules();
    $cart_rules_var_tpl_list = array();

    foreach($cart_rules as $rule){
      $vars = array();

      if($order->id_currency != $rule['reduction_currency']){
        $conversion_rate = floatval(Db::getInstance()->getRow("SELECT conversion_rate FROM ps_currency WHERE id_currency = {$order->id_currency}")['conversion_rate']);
        $vars['voucher_reduction'] = $rule['reduction_amount'] * $conversion_rate;
      }
      else{
        $vars['voucher_reduction'] = $rule['reduction_amount'];
      }

      $vars['voucher_reduction'] = Tools::displayPrice($vars['voucher_reduction'], $currency, false);
      $vars['voucher_name'] = $rule['description'];

      array_push($cart_rules_var_tpl_list, $vars);
    }

    $product_list_html = self::getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list);
    $cart_rules_list_html = self::getEmailTemplateContent('order_conf_cart_rules.tpl', Mail::TYPE_HTML, $cart_rules_var_tpl_list);  

    $data = array(
      '{products}' => $product_list_html,
      '{discounts}' => $cart_rules_list_html,
      '{firstname}' => $customer->firstname,
      '{lastname}' => $customer->lastname,
      '{email}' => $customer->email,
      '{delivery_block_txt}' => self::getFormatedAddress($delivery, "\n"),
      '{invoice_block_txt}' => self::getFormatedAddress($invoice, "\n"),
      '{delivery_block_html}' => self::getFormatedAddress($delivery, '<br />', array(
          'firstname' => '<span style="font-weight:bold;">%s</span>',
          'lastname' => '<span style="font-weight:bold;">%s</span>',
      )),
      '{invoice_block_html}' => self::getFormatedAddress($invoice, '<br />', array(
          'firstname' => '<span style="font-weight:bold;">%s</span>',
          'lastname' => '<span style="font-weight:bold;">%s</span>',
      )),
      '{delivery_company}' => $delivery->company,
      '{delivery_firstname}' => $delivery->firstname,
      '{delivery_lastname}' => $delivery->lastname,
      '{delivery_address1}' => $delivery->address1,
      '{delivery_address2}' => $delivery->address2,
      '{delivery_city}' => $delivery->city,
      '{delivery_postal_code}' => $delivery->postcode,
      '{delivery_country}' => $delivery->country,
      '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
      '{delivery_phone}' => ($delivery->phone) ? $delivery->phone : $delivery->phone_mobile,
      '{delivery_other}' => $delivery->other,
      '{invoice_company}' => $invoice->company,
      '{invoice_vat_number}' => $invoice->vat_number,
      '{invoice_firstname}' => $invoice->firstname,
      '{invoice_lastname}' => $invoice->lastname,
      '{invoice_address2}' => $invoice->address2,
      '{invoice_address1}' => $invoice->address1,
      '{invoice_city}' => $invoice->city,
      '{invoice_postal_code}' => $invoice->postcode,
      '{invoice_country}' => $invoice->country,
      '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
      '{invoice_phone}' => ($invoice->phone) ? $invoice->phone : $invoice->phone_mobile,
      '{invoice_other}' => $invoice->other,
      '{order_name}' => $order->getUniqReference(),
      '{date}' => Tools::displayDate(date('Y-m-d H:i:s'), null, 1),
      '{carrier}' => ($order->isVirtual() || !isset($carrier->name)) ?
          $this->module->trans('No carrier', array(), 'Admin.Payment.Notification') : $carrier->name,
      '{payment}' => Tools::substr($order->payment, 0, 255),
      '{total_paid}' => Tools::displayPrice($order->total_paid, $currency, false),
      '{total_products}' => Tools::displayPrice(
          Product::getTaxCalculationMethod() == PS_TAX_EXC ? $order->total_products :
              $order->total_products_wt,
          $currency
      ),
      '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
      '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
      '{total_shipping_tax_excl}' => Tools::displayPrice($order->total_shipping_tax_excl, $currency),
      '{total_shipping_tax_incl}' => Tools::displayPrice($order->total_shipping_tax_incl, $currency),
      '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
      '{total_tax_paid}' => Tools::displayPrice(($order->total_products_wt - $order->total_products) +
          ($order->total_shipping_tax_incl - $order->total_shipping_tax_excl), $currency),
    );

    $res = Mail::Send(
      (int)$order->id_lang,
      'order_conf',
      $context->getTranslator()->trans(
          'Order confirmation',
          array(),
          'Emails.Subject',
          $orderLanguage->locale
      ),
      $data,
      $customer->email,
      $customer->firstname.' '.$customer->lastname,
      null,
      null,
      false,
      null, _PS_MAIL_DIR_, false, (int)$order->id_shop
    );

    die(json_encode(array("success" => ($res ? true : false), "result" => $res)));
  }

  private static function getEmailTemplateContent($template_name, $mail_type, $var)
  {
    $email_configuration = Configuration::get('PS_MAIL_TYPE');
    if ($email_configuration != $mail_type && $email_configuration != Mail::TYPE_BOTH) {
        return '';
    }
    
    $pathToFindEmail = array(
        _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.Context::getContext()->language->iso_code.DIRECTORY_SEPARATOR.$template_name,
        _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.'en'.DIRECTORY_SEPARATOR.$template_name,
        _PS_MAIL_DIR_.Context::getContext()->language->iso_code.DIRECTORY_SEPARATOR.$template_name,
        _PS_MAIL_DIR_.'en'.DIRECTORY_SEPARATOR.$template_name,
    );

    foreach ($pathToFindEmail as $path) {
        if (Tools::file_exists_cache($path)) {
          Context::getContext()->smarty->assign('list', $var);
            return Context::getContext()->smarty->fetch($path);
        }
    }

    return '';
  }

  private static function getFormatedAddress(Address $the_address, $line_sep, $fields_style = array())
  {
    return AddressFormat::generateAddress($the_address, array('avoid' => array()), $line_sep, ' ', $fields_style);
  }

}

Here is a working controller that re-sends the email.

If you want you can take this and create a module by adding a re-send button in backoffice order showing hook.

I use it by code so I don't need a button, but feel free to add it and post the module here to help other people.

Pandal

Pandal

<?php

class [YourModuleName]ConfirmationEmailModuleFrontController extends ModuleFrontController
{
  public function initContent()
  { 
    if(!Tools::getValue("id_order")){
      die('Missing get parameter id_order');
    }

    self::sendOrderConfirmationEmail(intval(Tools::getValue("id_order")));
  }

  private static function sendOrderConfirmationEmail($id_order)
  {

    $order = new Order($id_order);
    $context = Context::getContext();
    $invoice = new Address($order->id_address_invoice);
    $delivery = new Address($order->id_address_delivery);
    $delivery_state = $delivery->id_state ? new State((int) $delivery->id_state) : false;
    $invoice_state = $invoice->id_state ? new State((int) $invoice->id_state) : false;
    $carrier = $order->id_carrier ? new Carrier($order->id_carrier) : false;
    $customer = $order->getCustomer();
    $orderLanguage = new Language((int) $order->id_lang);
    $currency = new Currency($order->id_currency);
    
    $product_var_tpl_list = array();
    
    foreach ($order->getProducts() as $product) {

      $vars = array();
      $vars['unit_price'] = Tools::displayPrice($product['unit_price_tax_incl'], $currency, false);
      $vars['price'] = Tools::displayPrice($product['total_price_tax_incl'], $currency, false);
      $vars['reference'] = $product['reference'];
      $vars['name'] = $product['product_name'];
      $vars['quantity'] = $product['product_quantity'];
      $vars['customization'] = array();

      array_push($product_var_tpl_list, $vars);
    }

    $cart = Cart::getCartByOrderId($order->id);

    $cart_rules = $cart->getCartRules();
    $cart_rules_var_tpl_list = array();

    foreach($cart_rules as $rule){
      $vars = array();

      if($order->id_currency != $rule['reduction_currency']){
        $conversion_rate = floatval(Db::getInstance()->getRow("SELECT conversion_rate FROM ps_currency WHERE id_currency = {$order->id_currency}")['conversion_rate']);
        $vars['voucher_reduction'] = $rule['reduction_amount'] * $conversion_rate;
      }
      else{
        $vars['voucher_reduction'] = $rule['reduction_amount'];
      }

      $vars['voucher_reduction'] = Tools::displayPrice($vars['voucher_reduction'], $currency, false);
      $vars['voucher_name'] = $rule['description'];

      array_push($cart_rules_var_tpl_list, $vars);
    }

    $product_list_html = self::getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list);
    $cart_rules_list_html = self::getEmailTemplateContent('order_conf_cart_rules.tpl', Mail::TYPE_HTML, $cart_rules_var_tpl_list);  

    $data = array(
      '{products}' => $product_list_html,
      '{discounts}' => $cart_rules_list_html,
      '{firstname}' => $customer->firstname,
      '{lastname}' => $customer->lastname,
      '{email}' => $customer->email,
      '{delivery_block_txt}' => self::getFormatedAddress($delivery, "\n"),
      '{invoice_block_txt}' => self::getFormatedAddress($invoice, "\n"),
      '{delivery_block_html}' => self::getFormatedAddress($delivery, '<br />', array(
          'firstname' => '<span style="font-weight:bold;">%s</span>',
          'lastname' => '<span style="font-weight:bold;">%s</span>',
      )),
      '{invoice_block_html}' => self::getFormatedAddress($invoice, '<br />', array(
          'firstname' => '<span style="font-weight:bold;">%s</span>',
          'lastname' => '<span style="font-weight:bold;">%s</span>',
      )),
      '{delivery_company}' => $delivery->company,
      '{delivery_firstname}' => $delivery->firstname,
      '{delivery_lastname}' => $delivery->lastname,
      '{delivery_address1}' => $delivery->address1,
      '{delivery_address2}' => $delivery->address2,
      '{delivery_city}' => $delivery->city,
      '{delivery_postal_code}' => $delivery->postcode,
      '{delivery_country}' => $delivery->country,
      '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
      '{delivery_phone}' => ($delivery->phone) ? $delivery->phone : $delivery->phone_mobile,
      '{delivery_other}' => $delivery->other,
      '{invoice_company}' => $invoice->company,
      '{invoice_vat_number}' => $invoice->vat_number,
      '{invoice_firstname}' => $invoice->firstname,
      '{invoice_lastname}' => $invoice->lastname,
      '{invoice_address2}' => $invoice->address2,
      '{invoice_address1}' => $invoice->address1,
      '{invoice_city}' => $invoice->city,
      '{invoice_postal_code}' => $invoice->postcode,
      '{invoice_country}' => $invoice->country,
      '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
      '{invoice_phone}' => ($invoice->phone) ? $invoice->phone : $invoice->phone_mobile,
      '{invoice_other}' => $invoice->other,
      '{order_name}' => $order->getUniqReference(),
      '{date}' => Tools::displayDate(date('Y-m-d H:i:s'), null, 1),
      '{carrier}' => ($order->isVirtual() || !isset($carrier->name)) ?
          $this->module->trans('No carrier', array(), 'Admin.Payment.Notification') : $carrier->name,
      '{payment}' => Tools::substr($order->payment, 0, 255),
      '{total_paid}' => Tools::displayPrice($order->total_paid, $currency, false),
      '{total_products}' => Tools::displayPrice(
          Product::getTaxCalculationMethod() == PS_TAX_EXC ? $order->total_products :
              $order->total_products_wt,
          $currency
      ),
      '{total_discounts}' => Tools::displayPrice($order->total_discounts, $currency),
      '{total_shipping}' => Tools::displayPrice($order->total_shipping, $currency),
      '{total_shipping_tax_excl}' => Tools::displayPrice($order->total_shipping_tax_excl, $currency),
      '{total_shipping_tax_incl}' => Tools::displayPrice($order->total_shipping_tax_incl, $currency),
      '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $currency),
      '{total_tax_paid}' => Tools::displayPrice(($order->total_products_wt - $order->total_products) +
          ($order->total_shipping_tax_incl - $order->total_shipping_tax_excl), $currency),
    );

    $res = Mail::Send(
      (int)$order->id_lang,
      'order_conf',
      $context->getTranslator()->trans(
          'Order confirmation',
          array(),
          'Emails.Subject',
          $orderLanguage->locale
      ),
      $data,
      '[email protected]',/*$customer->email*/,
      $customer->firstname.' '.$customer->lastname,
      null,
      null,
      false,
      null, _PS_MAIL_DIR_, false, (int)$order->id_shop
    );

    die(json_encode(array("success" => ($res ? true : false), "result" => $res)));
  }

  private static function getEmailTemplateContent($template_name, $mail_type, $var)
  {
    $email_configuration = Configuration::get('PS_MAIL_TYPE');
    if ($email_configuration != $mail_type && $email_configuration != Mail::TYPE_BOTH) {
        return '';
    }
    
    $pathToFindEmail = array(
        _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.Context::getContext()->language->iso_code.DIRECTORY_SEPARATOR.$template_name,
        _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.'en'.DIRECTORY_SEPARATOR.$template_name,
        _PS_MAIL_DIR_.Context::getContext()->language->iso_code.DIRECTORY_SEPARATOR.$template_name,
        _PS_MAIL_DIR_.'en'.DIRECTORY_SEPARATOR.$template_name,
    );

    foreach ($pathToFindEmail as $path) {
        if (Tools::file_exists_cache($path)) {
          Context::getContext()->smarty->assign('list', $var);
            return Context::getContext()->smarty->fetch($path);
        }
    }

    return '';
  }

  private static function getFormatedAddress(Address $the_address, $line_sep, $fields_style = array())
  {
    return AddressFormat::generateAddress($the_address, array('avoid' => array()), $line_sep, ' ', $fields_style);
  }

}

Here is a working controller that re-sends the email.

If you want you can take this and create a module by adding a re-send button in backoffice order showing hook.

I use it by code so I don't need a button, but feel free to add it and post the module here to help other people.

×
×
  • Create New...