PrestaShop $ this-> context-> smarty为null

时间:2017-04-10 22:35:27

标签: prestashop prestashop-1.6

我正在制作一个PrestaShop 1.6模块,并且发生了一些奇怪的事情。在该模块的配置页面中,我向用户提供了两种形式。一个用于配置自动电子邮件,另一个用于测试电子邮件。我的问题是,在提交第二个表单后,$this->context->smarty为空,给我这个错误:

Fatal error: Call to a member function assign() on null in /Users/andre/Projects/Web/xxx/modules/closecustomerthreademail/closecustomerthreademail.php on line 90

在第90行,我有:$this->context->smarty->assign('module_dir', $this->_path);,它位于此函数内部:

public function getContent()
{
    /**
     * If values have been submitted in the form, process.
     */
    if (((bool)Tools::isSubmit('submitClosecustomerthreademailModule')) == true) {
        $this->postProcess();
    }

    $this->context->smarty->assign('module_dir', $this->_path);
    $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');

    return $output.$this->renderForm();
}

这就是我在屏幕上添加两个表单的方法(renderForm方法):return $helper->generateForm(array($this->getConfigForm(), $this->getTestForm()));

postProcess。很抱歉粘贴了这么多代码,我试图提供我认为相关的所有细节。

protected function postProcess()
{
    if(Tools::isSubmit('test_form')) {
        $this->sendTestEmailTo(Tools::getValue('CLOSECUSTOMERTHREADEMAIL_EMAIL_TO_TEST'));
    }
    elseif(Tools::isSubmit('config_form')) {

        $form_values = $this->getConfigFormValues('config_form');

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }
}

sendTestEmailTo只有一个IF语句,然后调用此函数:

public function sendEmail($to_email, $to_name = null){

    $id_lang = $this->context->language->id;
    $template_dir = _PS_MODULE_DIR_.$this->name.'/views/templates/email/';

    $vars = array(
        '{html}' => Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_HTML_BODY'),
        '{text}' => Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_TEXT_BODY')
    );

    require(_PS_CONFIG_DIR_.'config.inc.php');
    require_once(_PS_ROOT_DIR_.'/init.php');

    $send = Mail::Send(
        (int)$id_lang,
        'email',
        Configuration::get('CLOSECUSTOMERTHREADEMAIL_EMAIL_SUBJECT'),
        $vars,
        $to_email,
        $to_name,
        null,
        null,
        null,
        null,
        $template_dir,
        false,
        (int)$this->context->shop->id,
        null
    );

    return $send;
}

我未能看到$this->context->smarty可能导致null的原因。你有任何提示可以帮我调查吗?

修改

构造方法:

public function __construct()
{
    $this->name = 'closecustomerthreademail';
    $this->tab = 'others';
    $this->version = '1.0.0';
    $this->author = 'andre';
    $this->need_instance = 0;

    /**
     * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
     */
    $this->bootstrap = true;

    parent::__construct();

    $this->displayName = $this->l('Close Customer Thread e-mail');
    $this->description = $this->l('Sends an e-mail whenever you close a customer thread');

    $this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');

    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}

2 个答案:

答案 0 :(得分:2)

我很高兴你已经解决了。但我猜这个问题是这样的:

require(_PS_CONFIG_DIR_.'config.inc.php');
require_once(_PS_ROOT_DIR_.'/init.php');

删除这些行:),你不应该永远不要在类方法中包含那些文件。

答案 1 :(得分:0)

在交付之前我无法理解,所以我必须这样解决,重定向:

protected function postProcess()
{
    if(Tools::isSubmit('test_form')) {
        $this->sendTestEmailTo(Tools::getValue('CLOSECUSTOMERTHREADEMAIL_EMAIL_TO_TEST'));
    }
    elseif(Tools::isSubmit('config_form')) {

        $form_values = $this->getConfigFormValues('config_form');

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

    $actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

    Tools::redirectAdmin($actual_link);
}