I want to redirect customer even if he is not logged in, on my custom page where customer need to add quote and details of product. He will redirect on this page when he click on ‘Add to Compare’ link from product view page and after submitting form he will stay over there as it is with successful message.
Please note that,this thing I have to done from preDispatch function of Index Controller.
Thank you.
You need to add code in config.xml
<frontend>
<events>
<controller_action_predispatch>
<observers>
<controller_action_before>
<class>dispatcher/observer</class>
<method>controllerActionPreDispatch</method>
</controller_action_before>
</observers>
</controller_action_predispatch>
</events>
</frontend>
Add code in model/observer.php file
public function controllerActionPreDispatch($observer)
{
//we compare action name to see if that's action for which we want to add our own event
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'yourmodule Action Name')
{
Mage::app()->getResponse()->setRedirect(Mage::getUrl("customer/account"));
}
}
Related
What I want to do is to empty the custom 'EAN' field of a product that gets copied in the backend. So as soon as the user hits 'copy' on an item the new items EAN field should be empty.
I found the magento event 'product_duplicate_attributes' but I am not sure if its what I need. Is there any way to fire an event if a product gets duplicated or maybe an even simpler solution to this problem.
Thanks in advance for any ideas.
(A): The field that should become empty, (B): Trigger event on save.
I found a solution to my problem:
etc/config.xml:
<models>
<ledscom_eanremover>
<class>LedsCom_EanRemover_Model</class>
</ledscom_eanremover>
</models>
</global>
<adminhtml>
<events>
<catalog_model_product_duplicate><!-- Observe product duplication. -->
<observers>
<ledscom_eanremover>
<class>ledscom_eanremover/observer</class>
<method>removeEan</method>
</ledscom_eanremover>
</observers>
</catalog_model_product_duplicate>
</events>
</adminhtml>
Model/Observer.php
<?php
class LedsCom_EanRemover_Model_Observer{
public function removeEan($observer){
$new_product = $observer->getEvent()->getNewProduct(); // Get new product from event-observer.
$new_product->setData('ean', null); // Remove the ean of the new product.
}
}
I am listening to the 'catalog_model_product_duplicate' event and remove the ean of the duplicated product.
I need to access it in an observer just before collectTotals() is run. I'm using the sales_quote_collect_totals_before observer and it seems to be doing everything else I need it to do. I want to be able to access this attribute when the event is triggered
You can use this sales_quote_address_collect_totals_before event in your module. like this:
<events>
<sales_quote_address_collect_totals_before>
<observers>
<namespace_your_modulename_custom_event>
<class>your_observer_class</class>
<method>yourObserverMethod</method>
</namespace_your_modulename_custom_event>
</observers>
</sales_quote_address_collect_totals_before>
</events>
In your observer model the method should look like this:
public function yourObserverMethod(Varien_Event_Observer $observer)
{
// code goes to here
}
I am building a module that adds a free product to the cart when a specific coupon code is entered.
I have an event observer which fires when a new coupon code is applied in the basket:
The event in my config is this:
<events>
<salesrule_validator_process>
<observers>
<checkoutApplyCouponToProduct>
<type>singleton</type>
<class>Sulman_Giftwithcoupon_Model_Checkout_Observer</class>
<method>applyCoupon</method>
</checkoutApplyCouponToProduct>
</observers>
</salesrule_validator_process>
</events>
This works correctly and I can add the free product succesully if the correct coupon code is added.
Now what I need to do is to remove the free product if the coupon code is canceled by the customer.
But the event I'm using does not fire when the coupon is canceled.
Is there an event I can use to check if the coupon code is removed?
Thanks
I would probably observe the controller_action_predispatch_checkout_cart_couponPost event and check whether the remove param is set, which is what triggers Mage_Checkout_CartController::couponPostAction to remove the coupon.
etc/config.xml
<config>
<global>
<events>
<controller_action_predispatch_checkout_cart_couponPost>
<observers>
<checkoutRemoveCouponProduct>
<type>singleton</type>
<class>Sulman_Giftwithcoupon_Model_Checkout_Observer</class>
<method>removeCoupon</method>
</checkoutRemoveCouponProduct>
</observers>
</controller_action_predispatch_checkout_cart_couponPost>
</events>
</global>
</config>
Model/Observer.php
public function removeCoupon(Varien_Event_Observer $observer)
{
/* #var Mage_Core_Controller_Front_Action $controller */
$controller = $observer->getControllerAction();
if ($controller->getRequest()->getParam('remove') == 1) {
// #TODO add logic to remove free product
}
return $this;
}
you can fire your custom event in couponPostAction() action in
app/code/core/Mage/Checkout/controllers/CartController.php
in this you will see a code which handles the wrong coupon and also if the coupon form has no value. You can fire your custom event there and listen that event in your module.
Hope this will do your work.
thanks
I'm new on magento and trying to write little extension for it (magento 1.7).
I have created tab in customer->edit, it prints multiselect, thats ok, the problem is that i cant get in observer file to catch multiselect options before saving and save them to my custom table in database.
there is some code snippets:
app/code/local/Gone/Brands/etc/config.xml
<adminhtml>
<layout>
<updates>
<customertab>
<file>customertab.xml</file>
</customertab>
</updates>
</layout>
<events>
<customer_save_after>
<observers>
<brands_hide_manufacturers>
<type>model</type>
<class>Gone_Brands_Model_Observer</class>
<method>saveHideManufacturers</method>
</brands_hide_manufacturers>
</observers>
</customer_save_after>
</events>
</adminhtml>
app/code/local/Gone/Brands/Model/Observer.php
<?php
class Gone_Brands_Model_Observer
{
public function _construct()
{
echo "ssssssssssssssssssssssssssss";
echo "<script>alert('aaa');</script>";
}
public function saveHideManufacturers() {
echo "ssssssssssssssssssssssssssss";
echo "<script>alert('bbbb');</script>";
}
}
Maye there are other solutions how to catch form from customer->edit -> my created tab with custom field?
Thank you.
==================================================================================
Finally, four hours spent for this. Maybe this helps for someone else.
config.xml
customer_save_after -> change to -> adminhtml_customer_save_after
Now works.
Finally, four hours spent for this. Maybe this helps for someone else.
config.xml
customer_save_after -> change to -> adminhtml_customer_save_after
Now works.
In magento you doesn't link to a class with its full class name.. you must use the framework :
replace :
<class>Gone_Brands_Model_Observer</class>
by :
<class>gonebrands/observer</class>
"gonebrands" (or whatever) refers to your XML node defining your MODEL layer in your config.xml
Event definition must be inside "global" tag. Also
you don't need a _construct method in your observer class
<global>
<events>
<customer_save_after>
<observers>
<brands_hide_manufacturers>
<type>singleton</type>
<class>Gone_Brands_Model_Observer</class>
<method>saveHideManufacturers</method>
</brands_hide_manufacturers>
</observers>
</customer_save_after>
</events>
</global>
I want to make my site private only to registered users. If any one try to access my homepage. the login screen should come first. People should register or login to enter into my site.
How to do this? Any Suggestion from magento experts?
If you do programming then simple logic would be checking if user is logged in or not else redirect them to the login page using event: controller_action_predispatch.
1> In your config.xml add the following xml code:
...
<frontend>
<events>
<controller_action_predispatch>
<observers>
<yourmodule_controller_action_predispatch>
<class>yourmodule/observer</class>
<method>checkForLogin</method>
</yourmodule_controller_action_predispatch>
</observers>
</controller_action_predispatch>
</events>
</frontend>
...
2> Create a Observer.php in your Module's Model/ directory.
Here is the basic code that you need to use in your Observer class:
<?php
class YourCompany_Yourmodule_Model_Observer extends Varien_Object{
public function checkForLogin(Varien_Event_Observer $observer){
if(!Mage::getSingleton('customer/session')->isLoggedIn() && $observer->getControllerAction()->getFullActionName() != 'customer_account_login') {
Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'))->sendResponse();
exit;
}
}
}
If you are interested with more robust solution with much more features ready to use for your B2B store then here is an awesome Magento Extension called 'Store Restriction Pro':
http://www.magepsycho.com/store-restriction-pro.html
Good Luck
Update (13-11-27), JSOV
In order to allow the forgot password, login access and default funcionality, you can add an array of the default pages you want to give permissions, and then validate and redirect.
ex:
...
$allow = array('customer_account_login','customer_account_forgotpassword','customer_account_resetpassword','customer_account_loginpost','customer_account_forgotpasswordpost','customer_account_resetpasswordpost');
if(!Mage::getSingleton('customer/session')->isLoggedIn() && !in_array(strtolower($observer->getControllerAction()->getFullActionName()),$allow) ) {
...