Magento - 如何自定义联系我们POST操作?

时间:2014-07-01 21:26:01

标签: php magento

我目前的Magento部署中定制了一个表单。自定义表单会点击contacts/index/post发送内容。

现在,他们要求我自定义,以便在成功发布表单后,会发生自定义重定向。但是,这意味着要编辑contacts/index/post入口点(并且会杀死一只小猫)。

我选择了备用路径:在创建目标网址后将目标网址更改为新网址(例如customcontacts/index/post)。

我应该在哪里输入以创建此类入口点以及我应该在哪里输入以获取contacts/index/post的内容(实际上:代码/脚本)(因此我可以将代码作为基础来创建我的自定义入口点)?

1 个答案:

答案 0 :(得分:1)

如果你有更改操作,那么你需要创建一个扩展which routing url is customcontacts并创建一个Indexcontroller.php并在contolers中创建动作postAction

有关如何创建模块http://www.amitbera.com/create-an-magento-extension-with-custom-database-table/的详细信息。

对于你工作修改过的config.xml是

<?xml version="1.0" ?>
<config>
    <modules>
        <Amit_Custommodule>
            <version>1.0.0</version>
        </Amit_Custommodule>
    </modules>
    <global>

        <!-- start of block -->
        <blocks>
            <custommodule>
                <class>Amit_Custommodule_Block</class>
            </custommodule>
        </blocks>
    </global>

    <!-- start of routers
    -->
    <frontend>
        <routers>
            <custommodule>
                <use>standard</use>
                <args>
                    <module>Amit_Custommodule</module>
                    <frontName>customcontacts</frontName>
                </args>
            </custommodule>
        </routers>
        </frontend>
</config>

Indexcontroller代码应该是

 <?php
class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{


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

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

    }
}

$这 - &GT; _redirect(&#39; / /&#39);是

如果表单页面是contacts / index /,那么

根据你的页面url进行更改

$这 - &GT; _redirect(&#39;触点/索引/&#39;) ; 要获得成功/失败消息,请在下面添加代码

表单页面<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>中的

必须检查

 $this->_initLayoutMessages('customer/session');
 $this->_initLayoutMessages('catalog/session');  

存在于表单动作控制器中 更

http://freegento.com/doc/d9/d7c/_contacts_2controllers_2_index_controller_8php-source.html