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

查看:89
本文介绍了向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命令行或首选的数据库操作方法,在newsletter_subscriber表中添加一个新列"country",例如varchar(100).

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 automaticly 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 customisation 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天全站免登陆