magento - 是否可以保存联系表格

时间:2014-04-17 11:16:00

标签: magento

只想问,是否有可能这样做?我正在考虑"硬编码"在Mage>Contacts>controllers> indexController.php中,输入字段中的数据将首先保存,然后邮寄给客户服务。

概述就像这样,

public function postAction() {
.
.
.
$translate = Mage::getSingleton('core/translate');
$connect = $resource->getConnection('extension_write');
$sql = 'INSERT INTO customer_callback '.
       '(date_created, name, phonenumber, callback_wish, comment) '.
       'VALUES (NOW(), "' . $post['name'] .'", "' . $post['phonenumber'] . '", callback_wish, comment)';
$retval = mysql_query( $sql, $connect );
.
.
.
}

1 个答案:

答案 0 :(得分:0)

Magento不提供保存contact us details to a database

如果要将其保存到数据中,则需要create a model and override contact us controllers。 第1步:创建文件夹 Config.xml路径app/code/local/Amit/Customcontactus/etc/

此档案的代码:

<?xml version="1.0" encoding="utf-8"?>
<config>
        <modules>
            <Amit_Customcontactus>
                <version>1.0.1</version>
            </Amit_Customcontactus>
        </modules>
    <global>
       <!-- Call Model -->
    <global>
        <models>
            <customcontactus>
                <class>Amit_Customcontactus_Model</class>
                <resourceModel>customcontactus_resource</resourceModel>
            </customcontactus>
            <customcontactus_resource>
                <class>Amit_Customcontactus_Model_Resource</class>
                <entities>
                    <customcontactus>
                    <table>customer_callback</table>
                    </customcontactus>
                </entities>
            </customcontactus_resource>
        </models>
        <resources>
            <customcontactus_setup>
                <setup>
                    <module>Amit_Customcontactus</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </customcontactus_setup>
            <customcontactus_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </customcontactus_read>
            <customcontactus_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </customcontactus_write>
        </resources>
        <!-- helper -->
        <helpers>
            <customcontactus>
                <class>Amit_Customcontactus_Helper</class>
            </customcontactus>
        </helpers>
    </global>
        <frontend>
            <routers>
                <customcontactus>
                    <use>standard</use>
                    <args>
                        <module>Amit_Customcontactus</module>
                        <frontName>customcontactus</frontName>
                    </args>
                </customcontactus>
            </routers>
        </frontend>
        <global>
            <rewrite>
                <customcontactus>
                    <from><![CDATA[#^/contacts/index/#]]></from>
                    <to>/customcontactus/index/</to>
                </customcontactus>
            </rewrite>
        </global>

</config>
Customcontactus.php l

app/code/community/Amit/Customcontactus/Mode

<?php
class Amit_Customcontactus_Model_Customcontactus extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('customcontactus/customcontactus');
    }

}
Customcontactus.php

app/code/community/Amit/Customcontactus/Model/Resource/

<?php
class Amit_Customcontactus_Model_Resource_Customcontactus extends Mage_Core_Model_Resource_Db_Abstract
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('customcontactus/customcontactus', 'custommodule_id');
    }
}
Customcontactus.php

上的

app/code/community/Amit/Customcontactus/Model/Resource/个文件

<?php
class Amit_Customcontactus_Model_Resource_Customcontactus_Collection
extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    /**
     * Initialize collection model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('customcontactus/customcontactus');
    }
}

IndexController.php

app/code/community/Amit/Customcontactus/controllers/
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Contacts').DS.'IndexController.php';
class Amit_Customcontactus_IndexController extends Mage_Contacts_IndexController
{
    public function indexAction()
    {

            parent::indexAction();

    }
        public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                }

                $translate->setTranslateInline(true);
        $model=Mage::getModel("customcontactus/customcontactus");
        $model->setData(array('date_created'=>NOW(),'name'=>$post['name'],
            'phonenumber'=>$post['phonenumber'],'callback_wish'=>'callback_wish',
            'comment'=>'comment'));
        $model->save();


                Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
                $this->_redirect('*/*/');

                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

        } else {
            $this->_redirect('*/*/');
        }
    }

}

定义表sql文件install.php
       路径

app/code/community/Amit/Customcontactus/sql/customcontactus_setup/

我在表格的其他列中添加了一列... See more

           <?php
    $installer=$this;
    $installer->startSetup();

    $installer->run("
    -- DROP TABLE IF EXISTS {$this->getTable('customcontactus')};
    CREATE TABLE {$this->getTable('customcontactus')} (
      `customcontactus_id` int(11) unsigned NOT NULL auto_increment COMMENT 'Q&A ID',
     .... put here you sql other  coloums
      PRIMARY KEY (`custommodule_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

        ");
    $installer->endSetup();
    ?>

最后创建模块控制文件模块名称为Amit_Customcontactus.xml at app/etc/modules

 <?xml version="1.0"?>
    <config>
        <modules>
            <Amit_Customcontactus>
                <codePool>community</codePool>
                <active>true</active>
            </Amit_Customcontactus>
        </modules>
    </config>