Magento display something custom on account create page - php

I want to display a line at this url of magento:
index.php/customer/account/create/
I tried to put event in config.xml, in my module...
(I am using custom local module to customize my magento)
<controller_action_predispatch_customer_account_create>
<observers>
<namespace_modulename_controller_action_predispatch_customer_account_create_observer>
<type>singleton</type>
<class>Namespace_Modulename_Model_Observer</class>
<method>onCustomerAccountCreateBefore</method>
</namespace_modulename_controller_action_predispatch_customer_account_create_observer>
</observers>
</controller_action_predispatch_customer_account_create>
And when I echo in the function, in observer.php,
public function onCustomerAccountCreateBefore (Varien_Event_Observer $observer)
{
Mage::log('function triggered',null,'logs.log');
echo 'test';
}
It successfully logs everything, and it prints test on the screen, but the problem is, it prints test on the top of screen, I don't want it to print test on top of screen but somewhere in the middle like before the register form, above the register form or below it, but not on top of screen...

Related

Magento: adding link in head to every frontend page

I have a module that needs a canonical link injected into <head> on literally every page on frontend. Is there a way to do it? Currently, given my module doesn't need its own page on frontend, nor any controllers whatsoever, I have only set the helper in my config.xml. Now, I do have an xml in layout, but the problem is that I need to change canonical link attributes based on user input(in admin), so XML doesn't fit. Yes, I could indeed fopen said fronted layout xml file, then replace what I need, then write new content back to it, but I wanted to check first whether there's other way for achieving that.
You can hook in on the core_block_abstract_prepare_layout_before event and use the Head block's addLinkRel method to add the link tag.
In your config.xml you need to define an observer like so:
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<your_module>
<class>Your_Module_Model_Observer</class>
<method>addCanonicalLink</method>
</your_module>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
Create an observer class in the Model directory
<?php
class Your_Module_Model_Observer {
public function addCanonicalLink(Varien_Event_Observer $observer) {
$block = $observer->getData('block');
if ($block->getNameInLayout() === 'head') {
// this will add <link rel="canonical" href="http://your-url.com">
$block->addLinkRel('canonical', 'http://your-url.com');
// If you need more attributes on the link tag use addItem instead
// This will add <link rel="canonical" href="http://your-url" attr="Your Attribute">
// $block->addItem('link_rel', 'http://your-url', 'rel="canonical" attr="Your Attribute"')
}
}
}
Update:
Since the core head.phtml template files run echo $this->getChildHtml() (render all children) it is possible to insert tags by adding a core/text block as a child and add a text string to it (just like you already tried with xml). If addItem doesn't fit your needs, this is more flexible as you can insert any string this way. Replace thie $block->addLinkRel line with
$canonical = $block->getLayout()->createBlock('core/text')
->setText('<link rel="canonical" href="http://your-url.com">')
$block->append($canonical);

Magento multisite - display of product in main site and subsite but order it only through the subsite

we have a subdomain multisite setup. We would like to have products listed in the both the main site (containing many brands/manufacturers) and a separate subsite that caters to that brand specifically.
A visitor to the main site can perform a product search or browse the categories to locate any products. If they click on a product to view it’s information/order, they will be directed to the subsite to have the product display there
Ordering the item will then take them through the cart process of the subsite with unique shipping extensions etc to that site.
Is this possible? Surely if the products are listed in the root site category but display in both, we should be able to tweak the scripta so that is directs to the subsite to order...
We’re not expecting this to be possible without customization, but not sure how to go about it.
You want to redirect to the brand site exactly when the product page on the main site is accessed. So what you can do is write an observer for the event controller_action_postdispatch_catalog_product_view, check if the current website is the main website and if the product is available on another website. If yes, redirect to this website. I choose postdispatch over predispatch because at this point the product is already loaded and we can easily retrieve the website information.
To do so, create a new module (I spare the basics here), configure the observer:
config.xml
<frontend>
<events>
<controller_action_postdispatch_catalog_product_view>
<observers>
<your_module>
<class>your_module/observer</class>
<method>redirectToBrandSite</method>
</your_module>
</observers>
</controller_action_postdispatch_catalog_product_view>
</events>
</frontend>
and implement the logic from above:
Observer.php
class Your_Module_Model_Observer
{
const MAIN_WEBSITE_ID = 1;
public function redirectToBrandSite(Varien_Event_Observer $observer)
{
if (Mage::app()->getWebsite()->getCode() != self::MAIN_WEBSITE_ID) {
return;
}
$product = Mage::registry('current_product');
if (!$product instanceof Mage_Catalog_Model_Product) {
return;
}
foreach ($product->getWebsiteIds() as $websiteId) {
if ($websiteId != self::MAIN_WEBSITE_ID) {
$redirectUrl = $product->setStoreId(
Mage::app()->getWebsite($websiteId)->getDefaultStore()->getId()
)->getProductUrl();
$observer->getControllerAction()->getResponse()->setRedirect($redirectUrl);
return;
}
}
}
You have to set the MAIN_WEBSITE_ID constant to the id of your main website (not the code). You can look that up in the core_website database.
You can create multiple store to achieve main website and sub site. My be you need to create URL redirections to redirect products to subsite.

Fullcalendar not rendering inside Magento block

I have a custom block within Magento Community which rewrites the Orders Tab in Adminhtml and replaces it with a Fullcalendar(arshaw.com's one) which displays events for when products are to be shipped.
I have implemented a JSON feed which relies on my custom modules controller to populate and save the data. This works beautifully and I can see the products once I click on Month / Today / Prev / Next. However, this is not how it should work. It should render when the page loads, except all I see are the buttons.
I have tried referencing the tab in javascript which it is in eg.
jQuery('#diagram_tab_orders_content').tabs({
show: function(event, ui) {
jQuery('#dashCalendar').fullCalendar('render');
}
Where #dashCalendar is the id / class of my tag.
Except this does not render the calendar. It does nothing but change the CSS of diagram_tab_orders_content.
I've seen a similar topic on Stackoverflow where you must explicitly reference the tab where the calendar is in order for it to render properly. This is what I'm trying to achieve in order for the calendar to render when the document is ready.
Any suggestions on how to make this calendar render when the document is ready would be much appreciated.
So I've figured out the issue with the calendar and it pertains to the tabs within Magento. Referencing the tabs explicitly within Javascript causes issues with the rendering, thus I had to rewrite the Dashboard block.
Config.xml in my ModuleName/etc folder:
<global>
<!-- Blocks -->
<blocks>
<adminhtml>
<rewrite>
<dashboard>ModuleName_Block_Adminhtml_Calendar</dashboard>
</rewrite>
</adminhtml>
</blocks>
</global>
Then within my ModuleName/Block/Calendar.php file I set the template:
public function __construct()
{ //this creates the template block for the calendar
parent::__construct();
$this->setId('Calendar');
$this->setTemplate('moduleName/calendar/calendar.phtml');
}
Copy/Paste the fullcalendar's JSON Example page provided, set your events: tag to your .php file which generates / requests data from your database using JSON. Customise your Theme if you want using the provided CSS and you will see the calendar render with the events when you log into your Adminhtml. Below you will see 'Populate'. Its an action within my Controller which retrieves the data and echo's out a JSON feed to the Calendar.
events: {
url: "<? echo $this->getUrl('moduleName/adminhtml_calendar/populate') ?>"
},
Now the Calendar renders with events properly.

What are the possibilities to add generated html from module in magento?

I am trying to debug external module in Magento. In that, when i logged in admin end, I have got pop up with some message and also got one notification message.Installed module generate the code for script to alert the popup message,div to display the notification. This HTML is generated and appended in DASHBOARD page which is having body class as " adminhtml-dashboard-index"
I guess there might be some way to push the generated code with dashboard page.
What are the possible ways to add external module notification or some html or generated js script with core module like DASHBOARD?
Hope you understand my question...
Thanks in advance
Here's one way to drop a block in there. Observe the adminhtml_block_html_before event:
<events>
<adminhtml_block_html_before>
<observers>
<super_adminhtml_block_html_before>
<class>super/observer</class>
<method>beforeAdminHtml</method>
</super_adminhtml_block_html_before>
</observers>
</adminhtml_block_html_before>
</events>
Then, create a new block and append it to the notifications block.
public function beforeAdminHtml($observer)
{
$block = $observer->getEvent()->getBlock();
if ($block->getNameInLayout() == 'root') {
$extendBlock = $this->_createMyNoticeBlock();
if ($extendBlock) {
$block->getChild('notifications')->append($extendBlock);
}
}
}
I used this to insert my own HTML block where the notifications usually show within the header - shows not only on the dashboard but on all the admin pages.

Magento customer registration 'Honey Pot' captcha

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.

Categories