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

pix*_*ode 9 newsletter magento subscription

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

Rom*_*tko 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;
}
Run Code Online (Sandbox Code Playgroud)


更新:

这里是详细的文章解释如何添加国家字段 另外,我已经创建了一个免费模块,它可以在GitHub上找到

  • 这个解决方案更简单,应该奖励恕我直言 (3认同)
  • @ Xand94在这里您可以找到有关在Magento中创建观察者的信息http://stackoverflow.com/questions/2048458/what-is-the-correct-way-to-set-up-an-observer-in-magento.这里有更详细的解释http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method#customize_magento_using_eventobserver (2认同)

sil*_*lvo 23

您需要做一些事情来完成这项工作:

  1. 将数据的新列添加到相应的数据库表
  2. 确保magento将新字段保存到数据库
  3. 在管理后端显示数据
  4. 收到新的简报订阅时记录数据

这是你如何做所有这些事情:

广告.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
    (...)
}
Run Code Online (Sandbox Code Playgroud)

广告.3)

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

$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'   =>    '----'
));
Run Code Online (Sandbox Code Playgroud)

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

广告.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();
}
Run Code Online (Sandbox Code Playgroud)

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

在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(); 
Run Code Online (Sandbox Code Playgroud)