mage registry key "_singleton/" already exists - magento

I know there are lot of posts with this problem, but I guess each of them is with different roots of it (at least from what I checked - nothing helped me).
I am trying to fire the event upon click on a button from the user, but I get the upper mentioned exception in a browser alert Mage registry key "_singleton/" already exists.
The part of the config.xml:
.....
<models>
<packagecustomernumber>
<class>Package_CustomerNumber_Model</class>
</packagecustomernumber>
</models>
</global>
<frontend>
<events>
<checkout_type_onepage_save_order>
<observers>
<type>singleton</type>
<class>packageName/customernumber/observer</class>
<method>setCustomerNumber</method>
</observers>
</checkout_type_onepage_save_order>
</events>
</frontend>
And the class itself:
class Package_CustomerNumber_Model_Observer
{
public function setCutomerNumber($observer)
{
die('setCutomerNumber');
}
}
The button which should fire the even it checking out/saving the order, so the event should be correct.
Any suggestions ?

The first thing that pops out is this
<class>packageName/customernumber/observer</class>
That's invalid. This is the node where you're telling Magento what class to use for your observer. As such, the <class/> node should be either the full PHP class name of your observer
<class>Package_CustomerNumber_Model_Observer</class>
Or a class aliases for the model
<class>packagecustomernumber/observer</class>
Also, before running your observer, it helps to make sure you can instantiate your model class. Try running the following code in a Magento loaded environment (script, controller action, phtml template, etc.)
$model = new Package_CustomerNumber_Model_Observer;
var_dump(get_class($model));
$model = Mage::getModel('packagecustomernumber/observer');
var_dump(get_class($model));
If you can't instantiate the class, then Magento won't be able to either (and it's easier to test this first before running through some steps to trigger your observer).

Yes, the "packageName/customernumber/observer" is the source of the problem.
while this class reference is completely incorrect in its structure, the problem actually comes up when your class reference does not match up with your global/models/modulename definition. even when the reference "looks" correct.
The config :
<config>
<global>
<models>
<mymodule>
<class>My_Module_Model</class>
</mymodule>
</models>
<events>
<some_event_tag>
<observers>
<my_event_observer_method>
<class>my_module/observer</class>
<method>myEventObserverMethod</method>
</my_event_observer_method>
</observers>
</some_event_tag>
</events>
</global>
</config>
Will have the same result because "my_module/observer" is not found, since the "my_module" class group node is not configured. The correct use for this sample would have been "mymodule/observer".
So if you run across this error, re-read your config.xml.

Make sure that your config.xml models section contains
<!-- This says that string 'company_module' corresponds to Company_Module_Model pseudo-namespace in getModel() and getSingleton() calls. -->
<company_module>
<class>Company_Module_Model</class>
</company_module>
Otherwise you won't be able to make new model instance.

Related

Magento observer throwing error

I'm attempting to subscribe to the sales_order_place_after observer in Magento so that I can output order data. Unfortunately, anytime I try to log output to Mage::log, print_r, or even var_dump, I receive a User Error: Some transactions have not been committed or rolled back error.
It's probably also important to note that I'm using one step checkout and the checkout usually hangs (confirmation email still comes through). Shortly after the hang, I receive a PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 234881024 bytes) error. Can't seem to track down where or what is causing the memory leak... but it this error is only spit out if I am trying to output data.
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<InfinitelyWhite_OrderEvent>
<version>0.0.1</version>
</InfinitelyWhite_OrderEvent>
</modules>
<!-- Configure our module's behavior in the global scope -->
<global>
<!-- Defining models -->
<models>
<!--
Unique identifier in the model's node.
By convention, we put the module's name in lowercase.
-->
<infinitelywhite_orderevent>
<!--
The path to our models directory, with directory
separators replaced by underscores
-->
<class>InfinitelyWhite_OrderEvent_Model</class>
</infinitelywhite_orderevent>
</models>
<events>
<sales_order_place_after>
<observers>
<infinitelywhite_orderevent>
<class>infinitelywhite_orderevent/observer</class>
<method>exportNewOrder</method>
<type>singleton</type>
</infinitelywhite_orderevent >
</observers>
</sales_order_place_after>
</events>
</global>
Observer.php
class InfinitelyWhite_OrderEvent_Model_Observer
{
/**
* Magento passes a Varien_Event_Observer object as
* the first parameter of dispatched events.
*/
public function exportNewOrder(Varien_Event_Observer $observer)
{
Mage::log('reached export_new_order');
$order = $observer->getOrder();
Mage::log($order->getData());
return $this;
}
}
Any help would be greatly appreciated.
Nick Parson .....please removed singleton
<type>singleton</type>
Turns out the error was coming up because I was attempting to pull order information with $observer->getOrder(), when the correct approach is to use $observer->getEvent()->getOrder(). I was simply missing the getEvent().
I'm now able to extract data like so:
$order = $observer->getEvent()->getOrder();
$billing = $order->getBillingAddress();
$shipping = $order->getShippingAddress();
$customer = $order->getCustomer();
And then I can do something like:
$order->getData() or $customer->getData()
It's also important to note that because I switched up my observer event and I'm not using sales_model_service_quote_submit_success which stopped the nasty User Error: Some transactions have not been committed or rolled back error.
Can't comment yet, but what happens if you disable your observer? (Just to make sure it is the observer causing this issue).
If it is the observer, you could try replacing $order->getData() with $order->debug() and see if that helps

Magento newsletter_subscriber change_status_at

I've been looking for a while at other threads, several have been close to what I need but not quite.
When a guest or customer sign up for our newsletter the field change_status_at populates with a timestamp.
However, if the guest or customer unsubscribe, we would like the the change_status_at field to pickup the current timestamp.
This is important to use because we do not use the newsletter "feature" of Magento 1.7.0.2 CE. Rather we export the newletter_subscriber list to a company to send an email.
Thank you,
Dan
I just ran into this too. A customer wanted to know the date when a subscribe/unsubscribe action occurred. Looking around the internets I've found a few others who have run into this and some claim that it used to work in very old version of Magento.
I think what happened is that the field definition for change_status_at used to be setup to auto update as a default timestamp (i.e. ON UPDATE CURRENT_TIMESTAMP), but that was lost in some update. So there's no Magento code that writes to the field, because MySql is supposed to magically maintain it.
You could try updating the table definition to add the ON UPDATE CURRENT_TIMESTAMP back in (but I'm not in favor changing default model tables) or adding another field to be the default timestamp field.
Or a better solution would be to create a module with an observer to just add the date in when the subscription changes. Here's that might look like (warning - this code is just an example, there may be some syntax errors due to expunging my module's info) -
app/code/local/Myco/MyMod/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Myco_MyMod>
<version>1.0.0</version>
</Myco_MyMod>>
</modules>
<global>
<models>
<myco>
<class>Myco_MyMod_Model</class>
</myco>
</models>
<events>
<newsletter_subscriber_save_before>
<observers>
<mycomymod_observer_subscriber>
<type>singleton</type>
<class>Myco_MyMod_Model_Observer</class>
<method>setUpdateDate</method>
</mycomymod_observer_subscriber>
</observers>
</newsletter_subscriber_save_before>
</events>
</global>
</config>
===========
in app/code/local/Myco/MyMod/Modules/Observer.php
<?php
class Myco_MyMod_Model_Observer
{
public function setUpdateDate(Varien_Event_Observer $observer) {
$subscriber = $observer->getSubscriber();
$subscriber['change_status_at'] = (date("Y-m-d H:i:s", time()));
}
}

Magento Router Url - Need Hyphnated Path Name

Let's say I use a custom controller to have a url path/frontend name of
/customcategory
Well, obviously if I have a controller file named 'TestController.php' and indexAction
the url path would be
/customcategory/test/index
What I am trying to figure out is how I do re-name the Test Controller, or modify the config xml file, so I can have a hyphenated url from a controller file such as
/customcategory/test-section/index
I know that if I want /customcategory to be hyphenated, I can just modify the frontend tag in the config file. But the site I am building would benefit from a hyphenated controller route, the part that comes after /customcategory with keywords and I cannot get it to work nor can I find an example on google - as crazy as that may seem.
Thanks for your time.
What you are trying to do is possible using global rewrite in your custom module. You could pass all incoming request for /customcategory/* to a specific controller action. But you would have to manage your own route (base on the depth of your url path).
e.g www.MageIgniter.com/customcategory/path1/path2
config.xml
<global>
<rewrite>
<fancy_url>
<from><![CDATA[/customcategory\/(.*)/]]></from>
<to><![CDATA[customcategory/index/processroute/tagname/$1/]]></to>
<complete>1</complete>
</fancy_url>
<rewrite>
</global>
<frontend>
<routers>
<tagseo>
<use>standard</use>
<args>
<frontName>customcategory</frontName>
</args>
</tagseo>
</routers>
class MageIgniter_Customcategory_IndexController extends Mage_Core_Controller_Front_Action
{
public function processRoute(){
print_r($requestUri = Mage::app()->getRequest()->getRequestUri()); //path1/path2
print_r($this->getRequest()->getParam('tagname')); // path1
print_r($this->getRequest())
// do you custom logic here base on about request path explode('/', trim($requestUri,'/'))
}
...
For a working example see "Product Tags" section # http://www.contempospace.com/bedroom-furniture/wardrobe-closets/custom-closet-systems/isa-closet-system-shelves-hanging-walk-in-reach-in-closet.html
As far as I am aware you cannot add hypens in the url to match up to a filename. If you are trying to get a folder structure you can just add more paths to it.
For example if you wanted:
Namespace/CustomCategory/controller/test/SectionController.php
you could do:
/customcategory/test_section/index

Magento router: How can I catch parameters in all URLs?

Think of a small and basic affiliate system. I want an URL like
www.myshop.com/mynewproduct.html?afid=123
Every time afid is found in the URL, a method should be called (basically to save "afid" in the session and when the customer buys stuff, I want to track it).
You don't need a router for this. You'll want to setup an event listener that fires for every page load, and then access the variables in the request collection. The controller_front_init_routers event should do.
So, setup your module's config with the following
<global>
<events>
<controller_front_init_routers>
<observers>
<packagename_modulename_observer>
<type>singleton</type>
<class>Packagename_Modulename_Model_Observer</class>
<method>interceptMethod</method>
</packagename_modulename_observer>
</observers>
</controller_front_init_routers>
</events>
</global>
And then create the following class
app/code/local/Packagename/Modulename/Model/Observer.php
class Packagename_Modulename_Model_Observer {
public function interceptMethod($observer) {
$request = $observer->getEvent()->getData('front')->getRequest();
$afid = $request->afid;
//do whatever you want with your variable here
}
}
The interceptMethod can be named whatever you want.
I know this is a very old answer, but it is valid to mention we shouldn't use the controller_front_init_routers event if we intend to store those parameters in session, which is the scenario for the original question. For example, if you instantiate customer/session at this point you won't be able to perform a customer login anymore. Alan pointed this himself in http://alanstorm.com/magento_sessions_early. BTW, thanks Alan for this great article.

Magento Email Templates

How can I set different email templates for Customer Order Confirm email and Admin copy of the same.
I need to add some extra content for the Admin email copy.
Thanks
I am assuming that you are currently using the "copy" feature to send the admin email. Let me know if that's not the case. Because the same email is currently being sent to multiple recipients, it would be difficult to change the content for each recipient. You could send multiple emails with a little bit of code, though, which would allow you to use a different email template for each. This could be achieved by creating a new class:
class MyModule_Model_Sales_Order extends Mage_Sales_Model_Order {
/**
* Sending email with order data
*
* #return Mage_Sales_Model_Order
*/
public function sendNewOrderEmail() {
parent::sendNewOrderEmail();
/**
* Your admin email sending code here. Copy it out of the sendNewOrderEmail
* function in Sales_Order.
*/
return $this;
}
}
And then telling Magento to override the core class inside your module config:
<config>
<global>
<models>
<mymodule>
<class>MyModule_Model</class>
</mymodule>
<sales>
<rewrite>
<order>MyModule_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
You'll need to create the template you want and be sure that your overridden model uses that template instead.
Change tag catalog to sales and add namespace with class name, Eg: if namespace is Custom then add Custom_MyModule_Model and Custom_MyModule_Model_Sales_Order, so the resulting XML file would be:
<config>
<global>
<models>
<mymodule>
<class>Custom_MyModule_Model</class>
</mymodule>
<sales>
<rewrite>
<order>Custom_MyModule_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
you can check and edit the HTML files in app > locale > en_US > templates > email
Please keep in mind that you are overruling the Sales and not the Catalog.
So to prevent searching why the above code is not working use "sales" instead of "catalog" in the config.xml.
This extension does it: http://codecanyon.net/item/send-new-order-email-to-admin/3198802
Totally worth the $10 given all the time I wasted trying to get the other answer here working. I had just finished making my own custom module for the contact form, but the other solutions here didn't work.

Resources