I'm new in magento and I wonder how to create a form in a CMS page and receive the information by email.
I did some research on the internet but I did not find explicit answers.
cmscms-blockcontact-usformsmagento-enterprise
I'm new in magento and I wonder how to create a form in a CMS page and receive the information by email.
I did some research on the internet but I did not find explicit answers.
The https
redirection does not work for cms pages because the Router
that matches the cms pages (Mage_Cms_Controller_Router
) does not check if some page should be secure.
Here is a little workaround to achieve this. It involves changing the router for the cms pages.
You will have to create a module. Let's call it Easylife_Secure
:
For this you will need the following files:
app/etc/modules/Easylife_Secure.xml
- the declaration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Secure>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core />
<Mage_Cms /><!-- should depend on Mage_Cms -->
</depends>
</Easylife_Secure>
</modules>
</config>
app/code/local/Easylife/Secure/Controller/Router.php
- your new router
<?php
class Easylife_Secure_Controller_Router extends Mage_Cms_Controller_Router
{
public function match(Zend_Controller_Request_Http $request)
{
if (!Mage::isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl('install'))
->sendResponse();
exit;
}
$identifier = trim($request->getPathInfo(), '/');
$condition = new Varien_Object(array(
'identifier' => $identifier,
'continue' => true
));
Mage::dispatchEvent('cms_controller_router_match_before', array(
'router' => $this,
'condition' => $condition
));
$identifier = $condition->getIdentifier();
if ($condition->getRedirectUrl()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect($condition->getRedirectUrl())
->sendResponse();
$request->setDispatched(true);
return true;
}
if (!$condition->getContinue()) {
return false;
}
$page = Mage::getModel('cms/page');
$pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());
if (!$pageId) {
return false;
}
//this line checks if the page should be secure
$this->_checkShouldBeSecure($request, '/'.$identifier);
$request->setModuleName('cms')
->setControllerName('page')
->setActionName('view')
->setParam('page_id', $pageId);
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$identifier
);
return true;
}
protected function _checkShouldBeSecure($request, $path = '')
{
if (!Mage::isInstalled() || $request->getPost()) {
return;
}
if ($this->_shouldBeSecure($path) && !$request->isSecure()) {
$url = $this->_getCurrentSecureUrl($request);
if ($request->getRouteName() != 'adminhtml' && Mage::app()->getUseSessionInUrl()) {
$url = Mage::getSingleton('core/url')->getRedirectUrl($url);
}
Mage::app()->getFrontController()->getResponse()
->setRedirect($url)
->sendResponse();
exit;
}
}
protected function _shouldBeSecure($path)
{
return substr(Mage::getStoreConfig('web/unsecure/base_url'), 0, 5) === 'https'
|| Mage::getStoreConfigFlag('web/secure/use_in_frontend')
&& substr(Mage::getStoreConfig('web/secure/base_url'), 0, 5) == 'https'
&& Mage::getConfig()->shouldUrlBeSecure($path);
}
protected function _getCurrentSecureUrl($request)
{
if ($alias = $request->getAlias(Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS)) {
return Mage::getBaseUrl('link', true).ltrim($alias, '/');
}
return Mage::getBaseUrl('link', true).ltrim($request->getPathInfo(), '/');
}
}
The method match
is the same as the one in the cms router, it just has one line that checks if the path should be secure. The rest of the methods are copied from the standard router.
app/code/local/Easylife/Secure/etc/config.xml
- the configuration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Secure>
<version>0.0.1</version>
</Easylife_Secure>
</modules>
<global>
<events>
<controller_front_init_routers>
<observers>
<cms><!-- change the cms router -->
<class>Easylife_Secure_Controller_Router</class>
<method>initControllerRouters</method>
</cms>
</observers>
</controller_front_init_routers>
</events>
</global>
<frontend>
<secure_url><!-- list here your secure pages. one slash (/) and the page identifier -->
<customer_service_contact>/customer-service-contact</customer_service_contact>
</secure_url>
</frontend>
</config>
Clear the cache and you are done.
[EDIT]
This small extension works for the general case, for all cms pages, I added it here for 'posterity', but I agree with Alex on this. For your specific case, you should use the standard contacts page and customize its template or with additional blocks
It's happening because the action attribute of form gets set by the controller file of the module, which is not being called as you are placing it on a custom page. You'll need to add that action to the form for proper submit behavior of contact form. For a quick fix, if you have your form.phtml file in theme folder, you can change the form action in it as :
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
to
<form action="<?php echo Mage::getUrl('contacts/index/post'); ?>" id="contactForm" method="post">
Best Answer
First you login Magento admin -> CMS -> Pages
Select your page and input your HTML code as you normally would on any other information you include the page, then select Content tab and add it.
The below lines to add your cms -> page: