PrestaShop 1.6 将订单产品添加到自定义电子邮件模板

PrestaShop 1.6 add order product to custom email template

我从邮件文件夹中复制 order_conf.tpl 和 order_conf.txt 文件并重命名。现在将覆盖添加到 classes/order/OrderHistory.php

我将函数 sendemail() 更改为:

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');
    if (isset($result['template']) && Validate::isEmail($result['email'])) {
        ShopUrl::cacheMainDomainForShop($order->id_shop);

        $topic = $result['osname'];

/*----------------------
-START OF INSERTED CODE-
----------------------*/

        /* GET THE PRODUCTS */
        $order_details = $order->getProducts();
        $product_var_tpl_list = array();
        foreach ($order_details as $id => &$order_detail) { 
            $product_var_tpl = array(
                    'reference' => $order_detail['product_reference'],
                    'name' => $order_detail['product_name'].(isset($order_detail['product_attributes']) ? ' - '.$order_detail['product_attributes'] : ''),
                    'unit_price' => Tools::displayPrice($order_detail['unit_price_tax_incl'], $this->context->currency, false),
                    'price' => Tools::displayPrice($order_detail['total_price_tax_incl'], $this->context->currency, false),
                    'quantity' => $order_detail['product_quantity'],
                    'customization' => $order_detail['customizedDatas']
            );
            $product_var_tpl_list[] = $product_var_tpl;
        } // end foreach ($order_detail)

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

        /* GET THE DISCOUNTS */
        $cart_rules = $order->getCartRules();
        foreach ($cart_rules as $id => &$cart_rule) {
                $cart_rules_list[] = array(
                    'voucher_name' => $cart_rule['name'],
                    'voucher_reduction' => ($cart_rule['value'] != 0.00 ? '-' : '').Tools::displayPrice($cart_rule['value'], $this->context->currency, false)
                );
        }
        $cart_rules_list_txt = '';
        $cart_rules_list_html = '';
        if (count($cart_rules_list) > 0) {
                $cart_rules_list_txt = $this->getEmailTemplateContent('order_conf_cart_rules.txt', Mail::TYPE_TEXT, $cart_rules_list);
                $cart_rules_list_html = $this->getEmailTemplateContent('order_conf_cart_rules.tpl', Mail::TYPE_HTML, $cart_rules_list);
        }  

        /* GET ORDER DETAILS, delivery, invoice, amount... etc */
        $invoice_address = new Address((int)$order->id_address_invoice);
        $invoiceAddressPatternRules = Tools::jsonDecode(Configuration::get('PS_INVCE_INVOICE_ADDR_RULES'), true);
        $deliveryAddressPatternRules = Tools::jsonDecode(Configuration::get('PS_INVCE_DELIVERY_ADDR_RULES'), true);
        $country = new Country((int)$invoice_address->id_country);
        $delivery_address = null;
        $formatted_delivery_address = '';
        if (isset($order->id_address_delivery) && $order->id_address_delivery) {
            $delivery_address = new Address((int)$order->id_address_delivery);
        }
        $carrier = new Carrier((int)($order->id_carrier), $order->id_lang);

        /* ATTACH INFORMATION TO SMARTY VARIABLE*/
        $data = array(
            '{lastname}' => $result['lastname'],
            '{firstname}' => $result['firstname'],
            '{id_order}' => (int)$this->id_order,
                '{delivery_block_txt}' => AddressFormat::generateAddress($delivery_address, $deliveryAddressPatternRules, ', ', ' '),

                '{invoice_block_txt}' => AddressFormat::generateAddress($invoice_address, $invoiceAddressPatternRules, ', ', ' '),

                '{delivery_block_html}' => AddressFormat::generateAddress($delivery_address, $deliveryAddressPatternRules, '<br />',' ', array(
                    'firstname'    => '<span style="font-weight:bold;">%s</span>',
                    'lastname'    => '<span style="font-weight:bold;">%s</span>'
                )),

                '{invoice_block_html}' => AddressFormat::generateAddress($invoice_address, $invoiceAddressPatternRules, '<br />',' ', array(
                        'firstname'    => '<span style="font-weight:bold;">%s</span>',
                        'lastname'    => '<span style="font-weight:bold;">%s</span>'
                )),

                '{delivery_company}' => $delivery_address->company,

                '{delivery_firstname}' => $delivery_address->firstname,
                '{delivery_lastname}' => $delivery_address->lastname,
                '{delivery_address1}' => $delivery_address->address1,
                '{delivery_address2}' => $delivery_address->address2,
                '{delivery_city}' => $delivery_address->city,
                '{delivery_postal_code}' => $delivery_address->postcode,
                '{delivery_country}' => $delivery_address->country,
                '{delivery_state}' => $delivery_address->id_state ? $delivery_state->name : '',
                '{delivery_phone}' => ($delivery_address->phone) ? $delivery_address->phone : $delivery_address->phone_mobile,
                '{delivery_other}' => $delivery_address->other,                 
                '{invoice_company}' => $invoice_address->company,
                '{invoice_vat_number}' => $invoice_address->vat_number,
                '{invoice_firstname}' => $invoice_address->firstname,
                '{invoice_lastname}' => $invoice_address->lastname,
                '{invoice_address2}' => $invoice_address->address2,
                '{invoice_address1}' => $invoice_address->address1,
                '{invoice_city}' => $invoice_address->city,
                '{invoice_postal_code}' => $invoice_address->postcode,
                '{invoice_country}' => $invoice_address->country,
                '{invoice_state}' => $invoice_address->id_state ? $invoice_state->name : '',
                '{invoice_phone}' => ($invoice_address->phone) ? $invoice_address->phone : $invoice_address->phone_mobile,
                '{invoice_other}' => $invoice_address->other,

                '{order_name}' => $order->getUniqReference(),
                '{date}' => Tools::displayDate(date('Y-m-d H:i:s'), null, 1),
                '{carrier}' => (!isset($carrier->name)) ? Tools::displayError('No carrier') : $carrier->name,
                '{payment}' => Tools::substr($order->payment, 0, 32),

                '{products}' => $product_list_html,
                '{products_txt}' => $product_list_txt,

                '{discounts}' => $cart_rules_list_html,
                '{discounts_txt}' => $cart_rules_list_txt, 

                '{total_paid}' => Tools::displayPrice($order->total_paid, $this->context->currency, false),
                '{total_products}' => Tools::displayPrice(Product::getTaxCalculationMethod() == PS_TAX_EXC ? $order->total_products : $order->total_products_wt, $this->context->currency, false),

                '{total_discounts}' => Tools::displayPrice($order->total_discounts, $this->context->currency, false),
                '{total_shipping}' => Tools::displayPrice($order->total_shipping, $this->context->currency, false),
                '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $this->context->currency, false),

                '{total_tax_paid}' => Tools::displayPrice(($order->total_products_wt - $order->total_products) + ($order->total_shipping_tax_incl - $order->total_shipping_tax_excl), $this->context->currency, false)
        );
/*---------------------
!-END OF INSERTED CODE-
---------------------*/

        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);

        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;
}

protected function getEmailTemplateContent($template_name, $mail_type, $var)
{
    $email_configuration = Configuration::get('PS_MAIL_TYPE');
    if ($email_configuration != $mail_type && $email_configuration != Mail::TYPE_BOTH) {
    }

    $theme_template_path = _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.Context::getContext()->language->iso_code.DIRECTORY_SEPARATOR.$template_name;
    $default_mail_template_path = _PS_MAIL_DIR_.Context::getContext()->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)) {
        Context::getContext()->smarty->assign('list', $var);
        return Context::getContext()->smarty->fetch($default_mail_template_path);
    }
    return ' ';
}

在状态中,我创建了一个新的状态并添加了向客户发送新电子邮件的选项,当我更改状态时,电子邮件会连同数据一起发送给客户,但订单中缺少所有产品详细信息。

我不知道如何在我的覆盖文件中更正此产品。

您的上下文后台语言没有所需的模板。

复制文件:

/mails/en/order_conf_cart_rules.tpl
/mails/en/order_conf_cart_rules.txt
/mails/en/order_conf_product_list.tpl
/mails/en/order_conf_product_list.txt

收件人:

/mails/YOUR_BACKOFFICE_USER_LANG_ISO_CODE/order_conf_cart_rules.tpl
/mails/YOUR_BACKOFFICE_USER_LANG_ISO_CODE/order_conf_cart_rules.txt
/mails/YOUR_BACKOFFICE_USER_LANG_ISO_CODE/order_conf_product_list.tpl
/mails/YOUR_BACKOFFICE_USER_LANG_ISO_CODE/order_conf_product_list.txt