将自定义字段添加到Magento订阅模块

时间:2010-07-01 07:25:03

标签: magento newsletter subscription

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

4 个答案:

答案 0 :(得分:33)

如果您想为Magento简报订阅者添加一些自定义字段(例如 subscriber_name ),您应该执行以下操作:

  • newsletter_subscriber表格
  • 添加新列
  • 将文字输入添加到简报模板
  • newsletter_subscriber_save_before 事件创建观察员

在观察者中,您可以从请求中获取自定义字段的值,并将其分配给订阅者的对象:

public function newsletterSubscriberSave(Varien_Event_Observer $observer)
{
    $subscriber = $observer->getEvent()->getSubscriber();
    $name = Mage::app()->getRequest()->getParam('subscriber_name');

    $subscriber->setSubscriberName($name);

    return $this;
}


更新

以下是解释how to add Country field的详细文章 另外,我创建了一个免费模块,它可以在GitHub

上找到

答案 1 :(得分:23)

有一些事情你需要照顾这些工作:

  1. 将数据的新列添加到相应的数据库表
  2. 确保magento将新字段保存到数据库
  3. 在admin后端显示数据
  4. 收到新的简报订阅时记录数据
  5. 以下是您可以做所有这些事情的方法:

    广告。 1)

    使用phpMyAdmin,mySQL命令行或您喜欢的任何数据库操作方法,将新的列“country”添加到newsletter_subscriber表中,例如varchar(100)。

    广告。 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)函数。 请务必首先制作文件的本地副本,而不是修改核心文件。以下是您需要添加的内容:

    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()的调用。您需要使用以下代码为“国家/地区”字段添加相应的调用:

    $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'   =>    '----'
    ));
    

    如果您希望该字段显示在网格的末尾(作为最后一列),请将其添加为最后一次调用,否则请将现有调用恰好放在您希望它最终位于管理员的位置。

    广告。 4)

    这是我在定制magento时事通讯时不必做的一部分,所以它主要是理论上的。订阅发生在位于app / code / core / Mage / Newsletter / controllers / SubscriberController.php的控制器中。这是我提议的更改的newAction方法的代码:

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

    完成上述步骤应该可以解决大部分问题。让我知道最后一部分是如何制定的,因为我没有机会对其进行测试。

    在Subscriber对象中有了附加字段后,您就可以随心所欲地执行任何操作。

    我真的不明白你的意思
      

    以电子邮件形式发送给预设收件人

    如果你能解释我也会尝试帮你解决这个问题。

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

    在将国家/地区添加到订阅者对象的部分之后,只需将以下代码添加到控制器即可。

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

答案 2 :(得分:0)

如果您要添加日期,日期时间或时间戳类型列,那么添加到已接受的答案中,您也可以更轻松地使用它。

在我的情况下,我想在我的网格中添加“订阅日期”。为此,我编写了升级脚本,列类型为TIMESTAMP,默认值为CURRENT_TIMESTAMP。这样,当添加行时,将记录当前日期/时间。

然后,您所要做的就是添加块自定义。我建议通过扩展Magento的网格块而不是执行本地代码池覆盖来实现。这样,您只需要覆盖_prepareColumns();

答案 3 :(得分:0)

旧帖子但是如果某人有相同的问题,则有一个免费扩展名,它会为性别,名字和姓氏添加字段,并使其在后端网格中可用,以便通过xml / csv导出:http://www.magentocommerce.com/magento-connect/extended-newsletter-subscription-for-guests.html

也许您可以扩展代码以满足您的需求。