Magento customer registration 'Honey Pot' captcha - php

I thought I had this one sorted but I have run into a snag. I want to add a 'Honey Pot' to the customer registration form, for those unfamiliar this technique involves hiding a text input using CSS and assumes that the average bot will want to fill it in. Humans however, will not see the field so it needs to validate as empty.
In Magento I created a new module, added the following to the config.xml:
<global>
<fieldsets>
<customer_account>
<honeytrap><create>1</create><update>1</update></honeytrap>
</customer_account>
</fieldsets>
<models>
<customer>
<rewrite>
<customer>MyStore_Honeytrap_Model_Customer</customer>
</rewrite>
</customer>
</models>
</global>
I then added a little bit extra to the validate function to check the field is empty. This all correct as far as I can see but at about line 278 in the AccountController.php the extractData() discards the input field from the post data in the request. I'm still very new to Magento so hoping to learn something here too but how do I prevent the field being stripped out of the post by extractData()?
Guess I just want to know what I'm missing, I've read a few posts on the internet regarding adding a custom field so as far as I know this should be working but maybe I've missed something out as I didn't include the Entity setup since I don't need to save this field in the database it's purely to validate the registration is from a human (as much as possible).
Thanks for any help, I'm sure it's probably something ridiculous that I've missed.
EDIT: Thanks to #gordon-knoppe pointer on using the event:
public function check_trap(Varien_Event_Observer $observer)
{
$event = $observer->getEvent();
$post = $event->getControllerAction()->getRequest()->getPost();
// Check Honeytrap is empty
if (Zend_Validate::is( trim($post['fname']) , 'NotEmpty'))
{
$customerHelper = Mage::helper('customer');
$error = $customerHelper->__('A problem has occured with your registration.');
Mage::getModel('customer/session')->addError($error);
Mage::app()->getResponse()
->setRedirect(Mage::getUrl('customer/account', array('_secure' => true)))
->sendResponse();
exit;
}
}
With this in the config.xml:
<events>
<controller_action_predispatch_customer_account_createpost>
<observers>
<mystore_honeytrap_observer>
<type>singleton</type>
<class>Mystore_Honeytrap_Model_Observer</class>
<method>check_trap</method>
</mystore_honeytrap_observer>
</observers>
</controller_action_predispatch_customer_account_createpost>
</events>

A more detached way to handle this could be to register an observer for before the relevant controller action (controller_action_predispatch_*) to detect whether your form field has been populated and, if so, redirect them out to prevent the native action from ever processing the request.

Related

Custom URL for magento extension

I am building a magento extension that will need a custom url for the frontend that is set via the admin panel config setting for the extension.
Along the lines of the "set admin url" setting in the system settings (which can also be set in the app/etc/local.xml file as well).
basically i have in my config.xml file
<frontend>
<routers>
<extensionname>
<use>standard</use>
<args>
<module>My_Extensionname</module>
<frontName>extensionname</frontName>
</args>
</extensionname>
</routers>
</frontend>
This creates the url site.com/extensionname
But I want to be able to set the url in the system/settings tab in the admin panel
I have looked through the core code and seen glimpses of code that does (a predispatch models controllers etc i think) this for the default admin url key setting
How would i go about this? Would i set up an observer to catch the request to url? or observer settings change and programmatically create a url rewrite?
What about the content & root template as well in the layout/extensionname.xml?
<layout version="0.1.0">
<extensionname_index>
<reference name="root">
<action method="setTemplate"><template>extensionname/page.phtml</template></action>
</reference>
<reference name="content">
<block type="extensionname/extensionname" name="extensionname" template="extensionname/extensionname.phtml" />
</reference>
</extensionname_index>
Would this still be used even though i would be using a custom url from the settings
Ok could not get any of the above info (links) to work from my end (in regards to using custom routers etc) because i think my extension is not using any collections from the database as its just a landing page so wont have index/index/id values etc.
So went with a dirty hack to do the job for now. See below.
etc/config.xml (observer event when admin field setting saved)
<config>
...
<frontend>
<routers>
<myextension>
<use>standard</use>
<args>
<module>Mycompany_Myextension</module>
<frontName>myextension</frontName>
</args>
</myextension>
</routers>
...
</frontend>
<global>
...
<events>
<admin_system_config_changed_section_myextension>
<observers>
<myextension>
<type>singleton</type>
<class>myextension/observer</class>
<method>observersave</method>
</myextension>
</observers>
</admin_system_config_changed_section_myextension>
</events>
...
</global>
</config>
Model/Observer.php (Save a URL Rewrite) (EDITED)
public function observersave(Varien_Event_Observer $observer)
{
#remove the old urlrewrite
$url = Mage::getStoreConfig('myextension/general/url');
$uldURLCollection = Mage::getModel('core/url_rewrite')->getResourceCollection();
$uldURLCollection->getSelect()
->where('id_path=?', 'myextension');//EDIT: so overwrites on each save
$uldURLCollection->setPageSize(1)->load();
if ( $uldURLCollection->count() > 0 ) {
$uldURLCollection->getFirstItem()->delete();
}
#add url rewrite
$modelURLRewrite = Mage::getModel('core/url_rewrite');
$modelURLRewrite->setIdPath('myextension/'.strtolower($url))
->setTargetPath('myextension/index/index/id/'.$url.'')
->setOptions('')
->setDescription('New URL - Created as a new setting was saved')
->setRequestPath('myextension/url/'.$url.'');//EDIT: added extra rewrite url paths so rewrite can never conflict if admin setting field is set to a "key default url" like "admin" or "checkout" or "contacts" etc
$modelURLRewrite->save();
}
controllers/IndexController.php (redirects if no ID...) (EDITED)
public function preDispatch()
{
//$url = Mage::getStoreConfig('myextension/general/url');
if ( !strstr($this->getRequest()->getRequestUri(), 'myextension/index/index/id') ) {
parent::preDispatch();
}
}
public function indexAction()
{
$url = Mage::getStoreConfig('myextension/general/url');
if ( trim($this->getRequest()->getParam('id')) == '' ) {
$this->_redirect('/');//Edit: changed redirect to root
} else {
$id = $this->getRequest()->getParam('id');
if($id == $url) {
$this->loadLayout( array(
'default',
'myextension_index_index'
));//EDIT: added if statement to see if myextension/index/index/id matched admin setting, if not redirect to root
$this->renderLayout();
}else{
$this->_redirect('/');//Edit: changed redirect to root
}
}
}
so at this point i have URLs at: (EDITED)
site.com/myextension/url/myadminfieldvalue
&
site.com/myextension/index/index/id/myadminfieldvalue
(EDITED)
but in template/myextension/myextension.phtml
<?php if($current_url == ''.$base_url.'myextension/index/index'){ ?>
<p>Disabled cause i dont really want info at this url..</p>
<?php }else{ ?>
<p>show data because your accessing myextension/index/index via the rewrite /myextension/url/myadminfieldvalue.</p>
<?php } ?>
A bit more work is needed to achieve what i want but for now it works for me and hope others may find this useful.

Magento: Check if product is already in cart

Using magento for my shopping cart system. I have mini cart on top menu, when I added product it will show product title with price in mini cart. Currently have product quantity edit link but I need quantity box in mini cart to update quantity with ajax. I have followed this link http://ceckoslab.com/magento/magento-check-if-product-is-in-cart/, but getting following error
Fatal error: Class 'Mage_Smartview_Helper_Data' not found in /app/Mage.php on line 547
can anyone help me to resolve above error ?
When Magento is trying to look to Mage_Somemodule_ instead of your own module, it does means that it did not found your own file or your own module.
Three possible reasons :
Your module is not recognized at all, maybe something is wrong in your module definition
Something is wrong/mistyped in your config.xml
Something is wrong/mistyped in your class name
Be sure you have everything right from the tutorial and/or copy paste your code here so we can help further
In this case the two xml are wrong in the way that it should not ignore case sensitivity
CeckosLab_SmartView.xml
<?xml version="1.0"?>
<config>
<modules>
<CeckosLab_SmartView>
<active>true</active>
<codePool>local</codePool>
</CeckosLab_SmartView >
</modules>
</config>
config.xml
<?xml version="1.0"?>
<config>
<modules>
<CeckosLab_SmartView>
<version>1.0.0</version>
</CeckosLab_SmartView>
</modules>
<global>
<helpers>
<smartview>
<class>CeckosLab_SmartView_Helper</class>
</smartview>
</helpers>
</global>
</config>
please check it the Answer of stackoverflow
Click Here
In this link check this answer with 20 votes
Please check the releasenotes:
It will help you guide how to solve this issue
If your helper error contains Mage_ before your module, it means your helper is either not defined or defined incorrectly in your module's xml or you are calling it incorrectly.
If it is defined correctly this should work:
Mage::helper('smartview')->doSomething();
or try:
Mage::helper('ceckoslab_smartview')->doSomething();
Also, make sure you clear the cache as xml is cached heavily.
Hope this helps
Make enrty in config.xml
<global>
......
<helpers>
<test>
<class>Module_Test_Helper</class>
</test>
</helpers>
......
</global>
Create a Data.php file inside Helper folder and write following code
<?php
class Module_Test_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Thats it you need to do now if you call this code it will not throw any error:
<?php Mage::helper("test")->actionname(); ?>

How to change URL on a magento extension?

I want to know how I can change the URL of my checkout cart. Whenever I continue to checkout or click on the checkout button, I get to the URL: mypage.com/onepagecheckout
I'm using this extension: http://www.magentocommerce.com/magento-connect/one-page-checkout.html
The only way I got it working was making one rewrite and a redirect, one rewrite from "onepagecheckout" to "checkout" and another one permanent redirect from "onepagecheckout" to "checkout". I don't think this method is best practice, so I would like to know if there's a better option.
I tried going to the app/code/community/IWD/OnepaceCheckout/etc/config.xml and changed frontname to "checkout", but even that did not change anything.
Thanks for your time and help would be greatly appreciated!
Please check in config.xml.here you found that onepagecheckout
just change onepagecheckout according
<frontend>
<routers>
.......
<use>standard</use>
<args>
<module>IWD_OnepaceCheckout/</module>
<frontName>onestepcheckout</frontName>
</args>
.......
</routers>
also changed it onestepcheckout.xml accoding to you wish frontName
Solved the problem by changing the in config.xml on both standard and admin.

Magento: How to write an extension with a block that overrides functionality and another new custom block?

I was in the middle of writing some code for a Magento website, and I feel like I'm at odds with what I am trying to accomplish.
I am trying to write an extension which inserts 2 blocks:
Hello_Catalog_Block_Category_View: which overrides the Mage_Catalog_Block_Category_View Block with some extra functionality.
Hello_Catalog_Block_Custom: which is a customised class I want to create for this extension
Here's what I have tried to write in the config.xml file:
<blocks>
<catalog>
<rewrite>
<category_view>Hello_Catalog_Block_Category_View</category_view>
</rewrite>
<class>
<custom>Hello_Catalog_Block_Custom</custom>
</class>
</catalog>
</blocks>
Obvously if I tried this code when I refresh the browser, this doesn't work because I must have initialised the custom block the wrong way.
Now if I tried to write in this fashion:
<blocks>
<catalog>
<rewrite>
<category_view>Hello_Catalog_Block_Category_View</category_view>
</rewrite>
<class>Hello_Catalog_Block</class>
</catalog>
</blocks>
Now when I refresh the browser, the templates for Catalog Category view don't get rendered and I get the feeling it gets overridden by <class>Hello_Catalog_Block</class>.
My question is, is there a way to write an extension that allows these 2 blocks to be used or together or would it just be a case where either you write an extension that overrides blocks or you write an extension that creates new blocks only, not both?
Thanks.
I think there's a disconnect between what you think a "custom block" will do and what they actually do. There's no way to just add something to config.xml and have the block show up on the page.
If you want to create a custom block for your module, the first step is to configure a new top level section under blocks
<blocks>
<hello_catalog>
<class>Hello_Catalog_Block</class>
</hello_catalog>
</blocks>
The <hello_catalog> node is you block's group name. When you use the above configuration, you're telling Magento
Hey Magento, if you see a block in the hello_catalog group, it's class name should start with Hello_Catalog_Block.
With the above in place, you'll be able to do things in Magento's layout update XML files (the XML files in app/design) like this
<block type="hello_catalog/custom" name="me_custom_block" />
The above XML is creating a block of type hello_catalog/custom. That's a block in the hello_catalog group, with its class name being custom. Magento will translate this into the full class name Hello_Catalog_Block_Custom (using the information from config.xml for the base name, and then lead-word-casing custom.

config.xml override results in cart not visible?

I am building a second add to cart button with a different redirect on a magento platform.
The tutorial I've used is self explainatory. See here
It works okey as the redirect is in place.
The thing is: The cart itself doesnt show its page (cart.phtml) anymore.
Any idea how to fix this? Or where the problem exists?
If I remove the code underneath the cart is back again, as the redirect isn't working anymore.
File : config.xml
<global>
<routers>
<checkout>
<rewrite>
<cart>
<to>mycheckout/cart</to>
<override_actions>true</override_actions>
<actions>
<add>
<to>mycheckout/cart/add</to>
</add>
</actions>
</cart>
</rewrite>
</checkout>
</routers>
</global>
Seems like your extension does not extend the cart controller that you are overwriting making it impossible to display the methods from original controller

Categories