Magento contact form on CMS page shows 404 error - magento

I added my contact form.phtml to a cms page with the following code:
{{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}}
However when I submit the contact form it will show a 404 error because the URL is being routed to www.domain.de/contacts/index/post, which doesn't exist because I use URL rewrites with store codes (de/en).
So the routing should go to www.domain.de/de/contacts/index/post
This only works when I alter the code as follows:
{{block type="core/template" name="contactForm" form_action="http://domain.de/de/contacts/index/post" template="contacts/form.phtml"}}
Since I am pretty new to magento I didn't find a solution and I dont know if this could be a problem with the .htaccess file.
Did anyone have the same issue and knows how to fix it?
Update:
Just to make sure if I did it correctly:
I created \app\code\local\MARG\ContactForm\etc\config.xml\ with the following content:
<config>
<modules>
<MARG_ContactForm>
<version>0.0.1</version>
</MARG_ContactForm>
</modules>
<global>
<blocks>
<ContactForm>
<class>MARG_ContactForm_Block</class>
</ContactForm>
<blocks>
</global>
</config>
Then I created app\code\local\MARG\ContactForm\Block\ContactForm.php with content:
class MARG_ContactForm_Block_ContactForm extends Mage_Core_Block_Template
{
protected function_construct()
{
$this ->setTemplate('contacts/form.phtml');
parent::_construct();
}
public function getFormAction()
{
return Mage::getUrl('*/*/post', array('_secure' => $this->getRequest()->isSecure()));
}
}
In app\etc\modules\MARG_ContactForm.xmlI put content:
<config>
<modules>
<MARG_ContactForm>
<active>true</active>
<codePool>local</codePool>
</MARG_ContactForm>
</modules>
</config>
In the CMS page I added {{block type="MARG/ContactForm" name="contactForm"}}
Is there something i overlooked?
Edit:
i have done it exactly as described and added {{block type="ContactForm/ContactForm" name="contactForm"}} to the CMS page. However it still shows a blank page the exception.log says
exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_ContactForm_Block_ContactForm' in C:\dev\plain\magento\app\Mage.php:595 Stack trace:
#0 C:\dev\plain\magento\app\code\core\Mage\Core\Model\Layout.php(495): Mage::throwException('Invalid block t...')
#1 C:\dev\plain\magento\app\code\core\Mage\Core\Model\Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('ContactForm/Con...', Array)

The problem here is that the block is type Mage_Core_Block_Template which doesn't have a function to turn form action into a fully qualified URL. Normally that happens in Mage_Contacts_IndexController but of course your CMS page is not using that controller.
The solution is to make a block type which suits your purpose. I'll assume you know how to make a module and that it's name is "example".
class Example_Example_Block_Contacts extends Mage_Core_Block_Template
{
protected function _construct()
{
$this->setTemplate('contacts/form.phtml');
parent::_construct();
}
public function getFormAction()
{
// copied from Mage_Contacts_IndexController::indexAction()
return Mage::getUrl('contacts/index/post', array('_secure' => $this->getRequest()->isSecure()));
}
}
This makes your CMS page simpler too. It only needs to include:
{{block type="example/contacts" name="contactForm"}}

Related

Need to remove the title from a specific magento category page

The problem
I need to remove the category name or title from specific non-product category pages but can't find the reference or block names to remove it with layout updates. I have already found the code that I could override and comment out to remove the title from every category page but that won't work since I need the title on most category pages.
What I'm trying to do
In the past I've been able to turn on the template path hints with block names, spend a second researching and found a great way to remove a block. This is the kind of code I have used before:
<reference name="Mage_Page_Block_Html_Breadcrumbs">
<remove name="breadcrumbs"/>
</reference>
TL;DR
I just need a simple way to remove the category title name from specific categories. If my idea about update xml is junk I'll take any suggestions. Thanks for any help.
I'd start by creating a custom module, overriding the Mage_Catalog_Block_Category_View block and doing something like this:
In app/local/Yournamespace/Titlemodule/etc/config.xml
<config>
<!-- .... -->
<global>
<blocks>
<titlemodule>
<class>Yournamespace_Titlemodule_Block</class>
</titlemodule>
<catalog>
<rewrite>
<product_view>Yournamespace_Titlemodule_Block_Category_View</product_view>
</rewrite>
</catalog>
</blocks>
<!-- ... -->
</config>
and in app/local/Yournamespace/Titlemodule/Block/Category/View.php
class Yournamespace_Titlemodule_Block_Category_View extends Mage_Catalog_Block_Category_View
{
protected function _prepareLayout()
{
parent::_prepareLayout();
$category = $this->getCurrentCategory(); // if needed
$headBlock = $this->getLayout()->getBlock('head');
// custom logic here
$headBlock->setTitle($this->__('New title, or none'));
return $this;
}
}
It will give you plenty of possibilities running custom logic before manipulating any blocks on the page.

How to edit tag url in magento?

I want to make SEO friendly tag URL in magento.
Currently it is abc.com/tag/product/list/tagId/17/
but i want to make it abc.com/tag/xyz
I tried this by using "URL rewrite management" but it is not working.
Please help.
First I want to say that this is a nice question. Got me all fired up.
It works with the url management but it's kind of a drag. To much work.
For example I added this in the url management.
Type : Custom
Store: Select any store here - if you have more you have to do this process for each one
ID Path: TAG_23
Request Path: tag/camera
Target Path: tag/product/list/tagId/23
Redirect: No
Saved. now when calling ROOT/tag/camera I see the prodcts tagged with 'camera'.
But for sure this is not the way to go. if you have more than 10 tags you get bored.
So the idea is to make a module that will make magento recognize tags like tag/something and will change the links for tags to the same format above, so you won't have to edit a lot of templates.
I named the module Easylife_Tag. You need for it the following files.
app/etc/modules/Easylife_Tag.xml - the declaration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Tag>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Tag />
</depends>
</Easylife_Tag>
</modules>
</config>
app/code/local/Easylife/Tag/etc/config.xml - the configuration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Tag>
<version>1.0.0</version>
</Easylife_Tag>
</modules>
<global>
<events>
<controller_front_init_routers><!-- add a custom router to recognize urls like tag/something -->
<observers>
<easylife_tag>
<class>Easylife_Tag_Controller_Router</class>
<method>initControllerRouters</method>
</easylife_tag>
</observers>
</controller_front_init_routers>
</events>
<models>
<tag>
<rewrite>
<tag>Easylife_Tag_Model_Tag</tag><!-- rewrite the tag model to change the url of the tags to tag/something -->
</rewrite>
</tag>
<tag_resource>
<rewrite>
<tag>Easylife_Tag_Model_Resource_Tag</tag> <!-- rewrite the tag resource model - see below why is needed -->
</rewrite>
</tag_resource>
</models>
</global>
</config>
app/code/local/Easylife/Tag/Model/Tag.php - the rewritten tag model
<?php
class Easylife_Tag_Model_Tag extends Mage_Tag_Model_Tag {
//change the url from `tag/product/list/tagId/23` to `tag/camera`
public function getTaggedProductsUrl() {
return Mage::getUrl('', array('_direct' => 'tag/'.$this->getName()));
}
}
app/code/local/Easylife/Tag/Model/Resource/Tag.php - rewritten tag resource model
<?php
class Easylife_Tag_Model_Resource_Tag extends Mage_Tag_Model_Resource_Tag {
//by default, when loading a tag by name magento does not load the store ids it is allowed in
//this method loads also the store ids
public function loadByName($model, $name){
parent::loadByName($model, $name);
if ($model->getId()) {
$this->_afterLoad($model);
}
else {
return false;
}
}
}
app/code/local/Easylife/Tag/Controller/Router.php - the custom router - see comments inline
<?php
class Easylife_Tag_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
public function initControllerRouters($observer){
$front = $observer->getEvent()->getFront();
$front->addRouter('easylife_tag', $this);
return $this;
}
public function match(Zend_Controller_Request_Http $request){
//if magento is not installed redirect to install
if (!Mage::isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl('install'))
->sendResponse();
exit;
}
//get the url key
$urlKey = trim($request->getPathInfo(), '/');
//explode by slash
$parts = explode('/', $urlKey);
//if there are not 2 parts (tag/something) in the url we don't care about it.
//return false and let the rest of the application take care of the url.
if (count($parts) != 2) {
return false;
}
//if the first part of the url key is not 'tag' we don't care about it
//return false and let the rest of the application take care of the url
if ($parts[0] != 'tag') {
return false;
}
$tagName = $parts[1]; //tag name
//load the tag model
$tag = Mage::getModel('tag/tag')->loadByName($tagName);
//if there is no tag with this name available in the current store just do nothing
if(!$tag->getId() || !$tag->isAvailableInStore()) {
return false;
}
//but if the tag is valid
//say to magento that the request should be mapped to `tag/product/list/tagId/ID_HERE` - the original url
$request->setModuleName('tag')
->setControllerName('product')
->setActionName('list')
->setParam('tagId', $tag->getId());
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$urlKey
);
return true;
}
}
That's it. Clear the cache and give it a go.
[EDIT].
You can find the full extension here. The only difference is that it uses the community code pool instead of local as described above.
Im using Magento 1.8.1 and tried the Marius's solution but I had one problem: 2+ keyword tags (with a space between words) was going to 404 page and the spaces in url were changed to %20. One keyword tag is working like a charm!
So, I modified his module to show a spaced word on Tag Module and 'hyphenized' in the URL.
File: Easylife/Tag/Model/Tag.php
return Mage::getUrl('', array('_direct' => 'tag/'.str_replace(" ", "-", $this->getName())));
File: Easylife/Tag/Controller/Router.php
$tagName = str_replace("-", " ", $parts[1]); //tag name
Its working for me now.
Best regards and thanks for the module Marius!

How to Protect user,for visit product page in magento

I am working in a Magento site, Requirement is when user comes to the site user should be redirect to login page,
Without visit any product page.
After register he will be able to view the products,
I have already tried but not getting any solution yet.
Anyone can help me on this?
You can simply use free available extension on magento connect. I used this extension for my store http://www.magentocommerce.com/magento-connect/login-check.html
It is free and doing job nice.
You can try the following approach as described here. Since posting single link answers is not recommended, here is what you need to do.
You need to create an observer for the event controller_action_predispatch for frontend. You can do that in a custom module. Let's call that module Easylife_Restrict.
You will need the following files:
app/etc/modules/Easylife_Restrict.xml - the declaration file.
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Restrict>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Customer />
</depends>
</Easylife_Restrict>
</modules>
</config>
app/code/local/Easylife/Restrict/etc/config.xml - the configuration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Restrict>
<version>1.0.0</version>
</Easylife_Restrict>
</modules>
<global>
<models>
<easylife_restrict>
<class>Easylife_Restrict_Model</class>
</easylife_restrict>
</models>
</global>
<frontend>
<events>
<controller_action_predispatch>
<observers>
<easylife_restrict>
<class>easylife_restrict/observer</class>
<method>redirectNotLogged</method>
</easylife_restrict>
</observers>
</controller_action_predispatch>
</events>
</frontend>
</config>
app/code/local/Easylife/Restrict/Model/Observer.php - the module observer - this is where the magic happens:
<?php
class Easylife_Restrict_Model_Observer{
public function redirectNotLogged(Varien_Event_Observer $observer)
{
//get the current request action
$action = strtolower(Mage::app()->getRequest()->getActionName());
//get the current request controller
$controller = strtolower(Mage::app()->getRequest()->getControllerName());
//a list of allowed actions for the not logged in user
$openActions = array(
'create',
'createpost',
'login',
'loginpost',
'logoutsuccess',
'forgotpassword',
'forgotpasswordpost',
'resetpassword',
'resetpasswordpost',
'confirm',
'confirmation'
);
//if the controller is the customer account controller and the action is allowed for everyone just do nothing.
if ($controller == 'account' && in_array($action, $openActions)) {
return $this; //if in allowed actions do nothing.
}
//if the user is not logged in redirect to the login page
if(! Mage::helper('customer')->isLoggedIn()){
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
}
}
}
Clear the cache and give it a try.
Please use this
Step 1: Go to Admin => System => Configuration => Customer Configuration => Login Options => "Redirect Customer to Account Dashboard after Logging in" set it "No".
Step 2: If you're using a module page then you've the controller Action method like:
public function indexAction()
{
// use here to restrict the page //
if(!Mage::helper('customer')->isLoggedIn()){
Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
$this->_redirect('customer/account/login');
}
// End your addition //
$this->loadLayout();
$this->renderLayout();
}
you can use one of this code to redirect:
Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
$this->_redirect('customer/account/login');
OR
$this->_redirect('customer/account/login/referer/'.Mage::helper('core')->urlEncode('Restricted Page Url'));
Step 3: (optional) If you're using a cms page then follow this step:
In your admin CMS section include a phtml page and write the below code at the top of the page:
if(!Mage::helper('customer')->isLoggedIn()){
Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
$this->_redirect('customer/account/login');
}
>> Now if you want to use the login url inside a page with referer url follow this:
<?php $referer = Mage::helper('core')->urlEncode(Mage::helper('core/url')->getCurrentUrl()); ?>
Login
I think it will solve your problem

getData doesn't work in Mage_Core_Block_Template custom block

I defined a custom block this way:
<frontend>
<layout>
<updates>
<categorylist module="mymodule">
<file>mymodule.xml</file>
</categorylist>
</updates>
</layout>
</frontend>
<global>
<blocks>
<categorylist>
<class>Alias_CategoryList_Block</class>
</categorylist>
</blocks>
</global>
Then I defined the block class this way
class Alias_CategoryList_Block_List extends Mage_Core_Block_Template
{
public $idCategory = NULL;
// Contructor
public function __construct()
{
echo $this->getData('categoryid');
}
}
and the layout this way:
<default translate="label">
<block type="categorylist/list" name="categorylist.list" output="toHtml" after="-" template="mymodule.phtml"/>
I put the block in a CMS this way:
{{block type="categorylist/list" categoryid="10"}}
But sadly the $this->getData('categoryid'); retrieves nothing.
Cannot figure out what's wrong ?
I tryed even $this->getCategoryid; but even nothing.
Anyone can help?
I'm using Magento 1.7
Would it be silly to add a view (phtml) template file that has
<?php echo $this->getCategoryId(); ?>
instead of trying to accomplish that in the constructor? Also then you wouldn't need your own code behind file you could just use core/template.
So your cms would be
{{block type="core/template" template="awesome.phtml" cateogryid="10"}}
The problem was that I assumed that the layout updates configuration will extend the code in the CMS but the code in the CMS should not have block name and template parameters for my purposes. So I fixed it declearing the template in the contructor and removing the layout updates in configuration (since I don't need the block to override existing blocks):
// Contructor
public function __construct()
{
$this->setTemplate('mymodule.xml');
}

Change Magento frontend router to custom URL

I would like to rename the URL path of the shopping cart from checkout/cart to checkout/adverts.
I have tried the following two methods:
Catalog > URL Rewrite Management: Custom
I have also tried doing it using a custom module, with the following in my config.xml:
<global>
<rewrite>
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart#]]></from>
<to>/checkout/adverts</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
Both methods have gone to the correct URL path, but shown a 404 Error page - is there something else I need to do? Magento ver. 1.5.0.1
What happen if you add a empty controller to your custom module
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Mynamespace_Mymodule_CartController extends Mage_Checkout_CartController
{
}
As R.S said you have to extend CartController.php in your module
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Mynamespace_Mymodule_CartController extends Mage_Checkout_CartController
{
}
?>
in it copy the indexAction() of Mage/Checkout/controllers/CartController.php
Also in your local.xml write
<mymodule_cart_adverts>
// copy the xml code inside <checkout_cart_index> ... </checkout_cart_index> here
</mymodule_cart_adverts>
I havent tried it but it should work

Resources