【问题标题】:Adding a custom field to Magento's subscription module向 Magento 的订阅模块添加自定义字段
【发布时间】:2011-03-10 12:08:26
【问题描述】:

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

【问题讨论】:

    标签: magento newsletter subscription


    【解决方案1】:

    要完成这项工作,您需要注意一些事项:

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

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

    广告。 1)

    使用 phpMyAdmin、MySQL 命令行或任何您喜欢的数据库操作方法,将新列“国家”添加到 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();
    }
    

    完成上述步骤应该可以解决大部分问题。让我知道最后一部分的效果,因为我没有机会测试它。

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

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

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

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

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

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

    【讨论】:

    • 感谢您的精彩回复。 “作为电子邮件发送..”表示我希望将订阅者信息(在这种情况下为电子邮件和国家/地区)作为电子邮件发送给管理员(例如 admin@mysite.com)。所以我将信息添加到数据库并作为电子邮件发送。谢谢
    • 您提到的文件中没有 _prepareSave 函数。我使用的是 1.4.0.1... 或者我只是遗漏了什么?
    • 我也缺少 _prepareSave 功能。实际上在 app/code/ 中的任何地方都找不到它
    • @silvo 我使用的是 1.7,但在您上面提到的文件中没有找到 _prepareSave。请让我知道该怎么做?提前致谢。
    • 抱歉,我不再使用 magento,因此无法评论最近版本的更改。希望其他用户可以参与...
    【解决方案2】:

    如果您想为 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

    【讨论】:

    • 这个解决方案更简单,应该奖励恕我直言
    • 这绝对是解决方案......必须进入 config.xml 什么代码才能让它工作?我知道它从前端开始> 但我不知道下一部分是什么..
    • 这适用于哪个版本的 Magento?我找不到订阅操作的任何事件。
    • @AdamMoss 此事件由Mage_Newsletter_Model_Subscriber 生成 - 您无法通过事件名称明确找到它。
    • @Xand94 在这里您可以找到有关在 Magento stackoverflow.com/questions/2048458/… 中创建观察者的信息。而这里更详细的解释magentocommerce.com/wiki/5_-modules_and_development/0-_module_development_in_magento/customizing_magento_using_event-observer_method#customize_magento_using_eventobserver
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      除了已接受的答案之外,如果您要添加日期、日期时间或时间戳类型的列,也可以更轻松地解决这个问题。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多