Jump to content

Email Custom template [shipped]


jacopocappelli

Recommended Posts

Hi,

 

You must add this variable in file /controllers/admin/AdminOrdersController.php

 

I can't find section for "shipped" state, i search all @Mail::Send but i can't find the right place where add $products array in fields.

Can you give me more details?

 

 

Thanks,

Jacopo

Link to comment
Share on other sites

1. Open file: /classes/order/OrderHistory.php

 

2. Before this: 

public function sendEmail($order, $template_vars = false)

Add this:

    /**
     * Fetch the content of $template_name inside the folder current_theme/mails/current_iso_lang/ if found, otherwise in mails/current_iso_lang
     *
     * @param string  $template_name template name with extension
     * @param int $mail_type     Mail::TYPE_HTML or Mail::TYPE_TXT
     * @param array   $var           list send to smarty
     *
     * @return string
     */
    protected function getEmailTemplateContent($template_name, $mail_type, $var)
    {
        if (!isset($this->context))
        {
            $this->context = Context::getContext();
        }

        $email_configuration = Configuration::get('PS_MAIL_TYPE');
        if ($email_configuration != $mail_type && $email_configuration != Mail::TYPE_BOTH)
        {
            return '';
        }

        $theme_template_path = _PS_THEME_DIR_ . 'mails' . DIRECTORY_SEPARATOR . $this->context->language->iso_code . DIRECTORY_SEPARATOR . $template_name;
        $default_mail_template_path = _PS_MAIL_DIR_ . $this->context->language->iso_code . DIRECTORY_SEPARATOR . $template_name;

        if (Tools::file_exists_cache($theme_template_path))
        {
            $default_mail_template_path = $theme_template_path;
        }

        if (Tools::file_exists_cache($default_mail_template_path))
        {
            $this->context->smarty->assign('list', $var);
            return $this->context->smarty->fetch($default_mail_template_path);
        }
        return '';
    }

3. After this:
 

        if (isset($result['template']) && Validate::isEmail($result['email']))
        {

add this:

            // add {products} to 'shipped.html' mail template
            if ($result['template'] == 'shipped')
            {
                $order = new Order((int)$this->id_order);
            
                foreach ($order->product_list as $product)
                {
                    $price = Product::getPriceStatic((int) $product['id_product'], false, ($product['id_product_attribute'] ? (int) $product['id_product_attribute'] : null), 6, null, false, true, $product['cart_quantity'], false, (int) $order->id_customer, (int) $order->id_cart, (int) $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
                    $price_wt = Product::getPriceStatic((int) $product['id_product'], true, ($product['id_product_attribute'] ? (int) $product['id_product_attribute'] : null), 2, null, false, true, $product['cart_quantity'], false, (int) $order->id_customer, (int) $order->id_cart, (int) $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});

                    $product_price = Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($price, 2) : $price_wt;

                    $product_var_tpl = array(
                        'reference' => $product['reference'],
                        'name' => $product['name'] . (isset($product['attributes']) ? ' - ' . $product['attributes'] : ''),
                        'unit_price' => Tools::displayPrice($product_price, $this->context->currency, false),
                        'price' => Tools::displayPrice($product_price * $product['quantity'], $this->context->currency, false),
                        'quantity' => $product['quantity'],
                        'customization' => array()
                    );

                    $customized_datas = Product::getAllCustomizedDatas((int) $order->id_cart);
                    if (isset($customized_datas[$product['id_product']][$product['id_product_attribute']]))
                    {
                        $product_var_tpl['customization'] = array();
                        foreach ($customized_datas[$product['id_product']][$product['id_product_attribute']][$order->id_address_delivery] as $customization)
                        {
                            $customization_text = '';
                            if (isset($customization['datas'][Product::CUSTOMIZE_TEXTFIELD]))
                            {
                                foreach ($customization['datas'][Product::CUSTOMIZE_TEXTFIELD] as $text)
                                {
                                    $customization_text .= $text['name'] . ': ' . $text['value'] . '<br />';
                                }
                            }

                            if (isset($customization['datas'][Product::CUSTOMIZE_FILE]))
                            {
                                $customization_text .= sprintf(Tools::displayError('%d image(s)'), count($customization['datas'][Product::CUSTOMIZE_FILE])) . '<br />';
                            }

                            $customization_quantity = (int) $product['customization_quantity'];

                            $product_var_tpl['customization'][] = array(
                                'customization_text' => $customization_text,
                                'customization_quantity' => $customization_quantity,
                                'quantity' => Tools::displayPrice($customization_quantity * $product_price, $this->context->currency, false)
                            );
                        }
                    }

                    $product_var_tpl_list[] = $product_var_tpl;
                    // Check if is not a virutal product for the displaying of shipping
                    if (!$product['is_virtual'])
                    {
                        $virtual_product &= false;
                    }
                } // end foreach ($products)

                $product_list_html = '';
                if (count($product_var_tpl_list) > 0)
                {
                    $product_list_html = $this->getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list);
                }

                $template_vars['products'] = $product_list_html;
            }

4. Add {products} in 'shipped.html' mail template.

 

 

I have not tested this so if it isn't working, enable DEBUG MODE and send me error messages.

 

Tip: To test this take the order and change state to 'shipped' in admin back-end.

Link to comment
Share on other sites

I've chaned this line:

$product_list_html = $this->getEmailTemplateContent('order_conf_product_list.tpl', Mail::TYPE_HTML, $product_var_tpl_list);

with this:

$product_list_html = $this->getEmailTemplateContent('shipped.html', Mail::TYPE_HTML, $product_var_tpl_list);

With debug active i don't see anything on php error log and mail arrive with:

{} instead of {products}

 

 

Thanks,

Jacopo

 

EDIT
this if:

if (count($product_var_tpl_list) > 0)

is not matched because count return 0 but in the order there is a product. 
Maybe it's a problem if order is a test order with many change of state?


EDIT 2
foreach work well, 

error_log('Lista Elementi ordine:'.count($order));
foreach ($order->product_list as $product)

say 1 as admin interface

Edited by jacopocappelli (see edit history)
Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...

Hi
Thank you for this information.

I have made some modifications to the original code and has served me as it works, I would like to share it with those who need it

 

This is the complete function:
 

public function sendEmail($order, $template_vars = false)
{
$result = Db::getInstance()->getRow('
SELECT osl.`template`, c.`lastname`, c.`firstname`, osl.`name` AS osname, c.`email`, os.`module_name`, os.`id_order_state`, os.`pdf_invoice`, os.`pdf_delivery`
FROM `'._DB_PREFIX_.'order_history` oh
LEFT JOIN `'._DB_PREFIX_.'orders` o ON oh.`id_order` = o.`id_order`
LEFT JOIN `'._DB_PREFIX_.'customer` c ON o.`id_customer` = c.`id_customer`
LEFT JOIN `'._DB_PREFIX_.'order_state` os ON oh.`id_order_state` = os.`id_order_state`
LEFT JOIN `'._DB_PREFIX_.'order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = o.`id_lang`)
WHERE oh.`id_order_history` = '.(int)$this->id.' AND os.`send_email` = 1');


// add {products} to 'shipped.html' mail template
if ($result['template'] == 'shipped')
{
$order = new Order((int)$this->id_order);
$products = $order->getProductsDetail();

foreach ($products as $product)
{
$price = Product::getPriceStatic((int) $product['id_product'], false, ($product['id_product_attribute'] ? (int) $product['id_product_attribute'] : null), 6, null, false, true, $product['cart_quantity'], false, (int) $order->id_customer, (int) $order->id_cart, (int) $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$price_wt = Product::getPriceStatic((int) $product['id_product'], true, ($product['id_product_attribute'] ? (int) $product['id_product_attribute'] : null), 2, null, false, true, $product['cart_quantity'], false, (int) $order->id_customer, (int) $order->id_cart, (int) $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$product_price = Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($price, 2) : $price_wt;

$product_var_tpl = array(
'reference' => $product['reference'],
'name' => $product['product_name'] . (isset($product['attributes']) ? ' - ' . $product['attributes'] : ''),
'unit_price' => Tools::displayPrice($product_price, $this->context->currency, false),
'price' => Tools::displayPrice($product_price * $product['product_quantity'], $this->context->currency, false),
'quantity' => $product['product_quantity'],
'customization' => array()
);

$customized_datas = Product::getAllCustomizedDatas((int) $order->id_cart);
if (isset($customized_datas[$product['id_product']][$product['id_product_attribute']]))
{
$product_var_tpl['customization'] = array();
foreach ($customized_datas[$product['id_product']][$product['id_product_attribute']][$order->id_address_delivery] as $customization)
{
$customization_text = '';
if (isset($customization['datas'][Product::CUSTOMIZE_TEXTFIELD]))
{
foreach ($customization['datas'][Product::CUSTOMIZE_TEXTFIELD] as $text)
{
$customization_text .= $text['name'] . ': ' . $text['value'] . '<br />';
}
}

if (isset($customization['datas'][Product::CUSTOMIZE_FILE]))
{
$customization_text .= sprintf(Tools::displayError('%d image(s)'), count($customization['datas'][Product::CUSTOMIZE_FILE])) . '<br />';
}

$customization_quantity = (int) $product['customization_quantity'];

$product_var_tpl['customization'][] = array(
'customization_text' => $customization_text,
'customization_quantity' => $customization_quantity,
'quantity' => Tools::displayPrice($customization_quantity * $product_price, $this->context->currency, false)
);
}
}

$product_var_tpl_list[] = $product_var_tpl;
// Check if is not a virutal product for the displaying of shipping
if (!$product['is_virtual'])
{
$virtual_product &= false;
}
} // end foreach ($products)

$product_list_html = '';
if (count($product_var_tpl_list) > 0)
{
foreach ($product_var_tpl_list as $product){
$product_list_html ='<tr>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td>
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$product['reference'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td>
<font size="2" face="Open-sans, sans-serif" color="#555454">
<strong>'.$product['name'].'</strong>
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td align="right">
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$product['unit_price'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td align="right">
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$product['quantity'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td align="right">
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$product['price'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
</tr>';}
foreach ($product_var_tpl_list['customization'] as $customization){
$product_list_html .='<tr>
<td colspan="2" style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td>
<font size="2" face="Open-sans, sans-serif" color="#555454">
<strong>'.$product['name'].'</strong><br>'.$customization['customization_text'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td align="right">
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$product['unit_price'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td align="right">
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$customization['customization_quantity'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
<td style="border:1px solid #EEEEEE;">
<table class="table">
<tr>
<td width="10"> </td>
<td align="right">
<font size="2" face="Open-sans, sans-serif" color="#555454">'.$customization['quantity'].'
</font>
</td>
<td width="10"> </td>
</tr>
</table>
</td>
</tr>';
}

}

// $template_vars['products'] = $product_list_html;
}

if (isset($result['template']) && Validate::isEmail($result['email'])) {
ShopUrl::cacheMainDomainForShop($order->id_shop);

$newDate = date("d-m-Y", strtotime($order->date_add));
$topic = $result['osname'];
$data = array(
'{lastname}' => $customer->lastname,
'{firstname}' => $customer->firstname,
'{id_order}' => (int)$this->id_order,
'{payment}' => $order->payment,
'{date}' => $newDate,
'{products}' => $product_list_html,
'{order_name}' => $order->getUniqReference()
);

if ($result['module_name']) {
$module = Module::getInstanceByName($result['module_name']);
if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars)) {
$data = array_merge($data, $module->extra_mail_vars);
}
}

if ($template_vars) {
$data = array_merge($data, $template_vars);
}

$data['{total_paid}'] = Tools::displayPrice((float)$order->total_paid, new Currency((int)$order->id_currency), false);
$data['{total_products}'] = Tools::displayPrice((float)$order->total_products_wt, new Currency((int)$order->id_currency), false);
$data['{total_discounts}'] = Tools::displayPrice((float)$order->total_discounts, new Currency((int)$order->id_currency), false);

if (Validate::isLoadedObject($order)) {
// Attach invoice and / or delivery-slip if they exists and status is set to attach them
if (($result['pdf_invoice'] || $result['pdf_delivery'])) {
$context = Context::getContext();
$invoice = $order->getInvoicesCollection();
$file_attachement = array();

if ($result['pdf_invoice'] && (int)Configuration::get('PS_INVOICE') && $order->invoice_number) {
Hook::exec('actionPDFInvoiceRender', array('order_invoice_list' => $invoice));
$pdf = new PDF($invoice, PDF::TEMPLATE_INVOICE, $context->smarty);
$file_attachement['invoice']['content'] = $pdf->render(false);
$file_attachement['invoice']['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
$file_attachement['invoice']['mime'] = 'application/pdf';
}
if ($result['pdf_delivery'] && $order->delivery_number) {
$pdf = new PDF($invoice, PDF::TEMPLATE_DELIVERY_SLIP, $context->smarty);
$file_attachement['delivery']['content'] = $pdf->render(false);
$file_attachement['delivery']['name'] = Configuration::get('PS_DELIVERY_PREFIX', Context::getContext()->language->id, null, $order->id_shop).sprintf('%06d', $order->delivery_number).'.pdf';
$file_attachement['delivery']['mime'] = 'application/pdf';
}
} else {
$file_attachement = null;
}

if (!Mail::Send((int)$order->id_lang, $result['template'], $topic, $data, $result['email'], $result['firstname'].' '.$result['lastname'],
null, null, $file_attachement, null, _PS_MAIL_DIR_, false, (int)$order->id_shop)) {
return false;
}
}

ShopUrl::resetMainDomainCache();
}

return true;
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...