A form was customized in my current Magento deploy. The customized form hits contacts/index/post
to send the contents.
Now, they asked me to customize that so when a successful form post occurs, a custom redirection occurs. However, that would imply editing the contacts/index/post
entry point (and a kitten would be killed).
I've chosen the alternate path: to change the target url to a new one, after creating it (e.g. customcontacts/index/post
).
Where should I enter to create such entry point and Where should I enter to get the contents (actually: code/script) of contacts/index/post
(so I can take the code as a base to create my custom entry point) ?
if you have change action then you need to create an extension which routing url is customcontacts
and create a Indexcontroller.php and create action postAction in contolers
More details on how create a module http://www.amitbera.com/create-an-magento-extension-with-custom-database-table/.
For you work modified config.xml is
<?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 code should be
<?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('*/*/');
}
}
}
$this->_redirect('//'); is
change according to you page url if form page is contacts/index/ then change this
$this->_redirect('contacts/index/') ; For getting success/failure message add below code
in form page <?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
Must check
$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');
is exist in form action controllers more
http://freegento.com/doc/d9/d7c/_contacts_2controllers_2_index_controller_8php-source.html