向 Magento 的订阅模块添加自定义字段 [英] Adding a custom field to Magento's subscription module

查看:29
本文介绍了向 Magento 的订阅模块添加自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Magento 中的时事通讯订阅模块默认只有一个字段(电子邮件).在向表单添加额外字段(例如国家/地区)后,如何让表单数据显示在 Magento 后端并作为电子邮件发送给预设收件人?谢谢.

The newsletter subscription module in Magento has only one field (email) by default. After I add an extra field to the form (say country), how can I get the form data to show up in the Magento back-end and be sent as an email to a preset recipient? Thanks.

推荐答案

要完成这项工作,您需要注意以下几点:

There are a few things that you need to take care of to make this work:

  1. 为您的数据添加一个新列到相应的数据库表
  2. 确保 Magento 将您的新字段保存到数据库中
  3. 在后台显示数据
  4. 在订阅新的时事通讯时记录数据

以下是您可以完成所有这些事情的方法:

Here's how you can do all those things:

广告.1)

使用 phpMyAdmin、MySQL 命令行或任何您喜欢的数据库操作方法,添加一个新列国家/地区";比如说,varchar(100) 到 newsletter_subscriber 表.

Using phpMyAdmin, MySQL command line, or whatever is your preferred DB manipulation method, add a new column "country" as, say, varchar(100) to the newsletter_subscriber table.

广告.2)

Magento 将自动让您通过 Mage_Newsletter_Model_Subscriber 对象上的 getCountry()setCountry() 方法访问新字段.它不会做的唯一一件事是在您的字段被系统中某处的代码更改后将其保存回数据库.要保存它,您需要修改 Mage_Newsletter_Model_Mysql4_Subscriber (app/code/core/Mage/Newsletter/Model/Mysql4/Subscriber.php) 中的 _prepareSave(Mage_Newsletter_Model_Subscriber $subscriber) 函数.请务必先制作文件的本地副本,不要修改核心文件.以下是您需要添加的内容:

Magento will automatically give you access to the new field through the getCountry() and setCountry() methods on the Mage_Newsletter_Model_Subscriber object. The only thing it won't do is save your field back to the DB after it has been changed with code somewhere in the system. To get it saved you need to modify _prepareSave(Mage_Newsletter_Model_Subscriber $subscriber) function found in Mage_Newsletter_Model_Mysql4_Subscriber (app/code/core/Mage/Newsletter/Model/Mysql4/Subscriber.php). Be sure to make a local copy of the file first and not modify the core file. Here's what you need to add:

protected function _prepareSave(Mage_Newsletter_Model_Subscriber $subscriber)
{
    $data = array();
    $data['customer_id'] = $subscriber->getCustomerId();
    $data['store_id'] = $subscriber->getStoreId()?$subscriber->getStoreId():0;
    $data['subscriber_status'] = $subscriber->getStatus();
    $data['subscriber_email']  = $subscriber->getEmail();
    $data['subscriber_confirm_code'] = $subscriber->getCode();

    //ADD A NEW FIELD START

    //note that the string index for the $data array
    //must match the name of the column created in step 1
    $data['country'] = $subscriber->getCountry();

    //ADD A NEW FIELD END
    (...)
}

广告.3)

您需要修改(本地副本)文件 app/code/core/Mage/Adminhtml/Block/Newsletter/Subscriber/Grid.php.您正在寻找的方法称为 _prepareColumns().在那里你会看到一系列对 $this->addColumn() 的调用.您需要为您的国家"添加相应的呼叫;带有以下代码的字段:

You will need to modify (a local copy of) the file app/code/core/Mage/Adminhtml/Block/Newsletter/Subscriber/Grid.php. The method you are looking for is called _prepareColumns(). In there you will see a series of calls to $this->addColumn(). You need to add a corresponding call for your "Country" field with the following code:

$this->addColumn('country', array(
    'header'    => Mage::helper('newsletter')->__('Country'),
    //the index must match the name of the column created in step 1
    'index'     => 'country',
    'default'   =>    '----'
));

如果您希望该字段出现在网格的末尾(作为最后一列),请将其添加为最后一个调用,否则,将其挤在现有调用之间,恰好是您希望它在管理中结束的位置.

If you want the field to appear at the end of the grid (as the last column) add it as the last call, otherwise, squeeze it between the existing calls exactly where you want it to end up in the admin.

广告.4)

这是我在定制 Magento 时事通讯时不必做的部分,因此主要是理论性的.订阅发生在位于 app/code/core/Mage/Newsletter/controllers/SubscriberController.php 的控制器中.这是带有我建议更改的 newAction 方法的代码:

This is a part I did not have to do in my customization of the Magento newsletter, so it will be mostly theoretical. The subscription occurs in the controller located at app/code/core/Mage/Newsletter/controllers/SubscriberController.php. Here's the code of the newAction method with my proposed changes:

public function newAction()
{
    if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
        $session   = Mage::getSingleton('core/session');
        $email     = (string) $this->getRequest()->getPost('email');

        try {
            if (!Zend_Validate::is($email, 'EmailAddress')) {
                Mage::throwException($this->__('Please enter a valid email address'));
            }

            $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
            if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                $session->addSuccess($this->__('Confirmation request has been sent'));
            }
            else {
                $session->addSuccess($this->__('Thank you for your subscription'));
            }
                
                //ADD COUNTRY INFO START
                
                //at this point we may safly assume that subscription record was created
                //let's retrieve this record and add the additional data to it
                $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                
                //assuming that the input's id is "country"
                $subscriber->setCountry((string) $this->getRequest()->getPost('country'));
                
                //don't forget to save the subscriber!
                $subscriber->save();
                
                //ADD COUNTRY INFO END
        }
        catch (Mage_Core_Exception $e) {
            $session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
        }
        catch (Exception $e) {
            $session->addException($e, $this->__('There was a problem with the subscription'));
        }
    }
    $this->_redirectReferer();
}

通过上述步骤应该可以解决大部分问题.让我知道最后一部分是如何完成的,因为我没有机会测试它.

Going through the above steps should take care of the most part of your problem. Let me know how that last part worked out, as I did not have a chance to test it.

一旦您在 Subscriber 对象中有您的附加字段,您就可以对它做任何想做的事情.我没有真正理解你的意思

Once you have your additional field in the Subscriber object you can do whatever you want with it. I did not really get what you mean by

作为电子邮件发送给预设的收件人

be sent as an email to a preset recipient

如果你能解释一下,我也会尽力帮助你完成这部分.

If you can explain that I will try to help you out with this part too.

编辑 - 如何在有人订阅时发送邮件

只需将以下代码添加到控制器中将国家添加到订阅者对象的部分之后即可.

Just add the following code to the controller after the part which adds country to a subscriber object.

$mail = new Zend_Mail();
$mail->setBodyHtml("New subscriber: $email <br /><br />Country: ".$this->getRequest()->getPost('country'));
$mail->setFrom("youremail@email.com")
->addTo("admin@mysite.com")
->setSubject("Your Subject here");
$mail->send(); 

这篇关于向 Magento 的订阅模块添加自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆