Prestashop 1.7.4.x - 使用 order_conf 作为新电子邮件模板时缺少变量值

问题描述 投票:0回答:4

使用新电子邮件模板时缺少变量。 我没有更改此电子邮件模板的任何核心代码。

有人可以帮助我吗? [我正在使用 Prestashop 1.7.4.x ]

enter image description here

enter image description here

php variables prestashop
4个回答
3
投票

最后我找到了一个解决方案。也许有人会从我的解决方案中获得帮助。 我修改了 classes/order/OrderHistory.php 并替换了以下代码。

$data = array(
            '{lastname}' => $result['lastname'],
            '{firstname}' => $result['firstname'],
            '{id_order}' => (int)$this->id_order,
            '{order_name}' => $order->getUniqReference()
        );

将上面的代码替换为以下代码。

/*----------------------
        -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-
        ---------------------*/ 

我还在当前类文件中添加了以下函数。

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

2
投票

游戏有点晚了,但我发现这个线程试图做同样的事情:在订单详细信息不可用的状态下使用“order_conf”模板。

根据 @viral 对他自己问题的回答,我遇到了一些问题,所以这就是我为让 Presta Shop V1.7.3.2 正常工作而所做的事情

创建文件[WEBROOT]/override/classes/order/OrderHistory.php 这样做,而不是更改核心代码,将允许您更新 Presta Shop,而不会丢失您的更改。

在该文件中我添加了以下内容:

<?php

/**
 * Make complete Order details available to email templates
 */

use PrestaShop\PrestaShop\Core\Stock\StockManager;
use PrestaShop\PrestaShop\Adapter\StockManager as StockManagerAdapter;

class OrderHistory extends OrderHistoryCore {
    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'];
            // JOHN
            $this->context = Context::getContext();
            /* 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_list = array();
            $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);
            $invoice = new Address((int)$order->id_address_invoice);
            $delivery = new Address((int)$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;

            /* 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 JOHN

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

    /**
     * Added by John
     */
    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) {
            return '';
        }

        $pathToFindEmail = array(
            _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.$this->context->language->iso_code.DIRECTORY_SEPARATOR.$template_name,
            _PS_THEME_DIR_.'mails'.DIRECTORY_SEPARATOR.'en'.DIRECTORY_SEPARATOR.$template_name,
            _PS_MAIL_DIR_.$this->context->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)) {
                $this->context->smarty->assign('list', $var);
                return $this->context->smarty->fetch($path);
            }
        }
        return 'Template Not Found';
    }
}

如果您在该代码中搜索“John”,您将找到标准“sendEmail”函数和重写版本的修改。

希望对某人有帮助。


0
投票

每次进行更改后,请擦除缓存。在 prod/class_index.php 中。我让它在1.7.3.3下工作


0
投票

我能够将变量添加到shipped.html 电子邮件模板中。唯一的问题是变量:{products} 和 {discounts} 未显示。大家有什么建议吗?

我使用仓库作为主题,prestashop版本是1.7.8.7

HTML 预览:https://ibb.co/X8x91K6 电子邮件结果:https://ibb.co/xSx5Pwb

正如您在电子邮件结果中看到的,变量未填充。

© www.soinside.com 2019 - 2024. All rights reserved.