2

I'm, trying to define and implement observers to catch comments on the checkout page and write them into the database. But when I want to place an order I get the following exception:

Mage registry key "_singleton/checkoutcomments/observer" already exists

Do I get this error because the model is not called properly?

Here is my code:

The config.xml of the module

<frontend>
    <layout>
        <updates>
            <checkoutcomments>
                <file>practice/checkoutcomments.xml</file>
            </checkoutcomments>
        </updates>
    </layout>

    <events>
        <controller_action_predispatch_checkout_onepage_saveOrder>
            <observers>
                <save_checkout_comments>
                    <class>checkoutcomments/observer</class>
                    <method>prepareCheckoutComment</method>
                </save_checkout_comments>
            </observers>
        </controller_action_predispatch_checkout_onepage_saveOrder>

        <sales_order_place_after>
            <observers>
                <save_checkout_comments>
                    <class>checkoutcomments/observer</class>
                    <method>saveCheckoutComment</method>
                </save_checkout_comments>
            </observers>
        </sales_order_place_after>
    </events>

</frontend>

My Observer.php

    <?php

class Practice_CheckoutComments_Model_Observer
{

    public function prepareCheckoutComment ($observer)
    {
        $message = $observer->getControllerAction()
            ->getRequest()
            ->getPost('checkoutcomments');
        Mage::getSingleton('core/session')->setCheckoutComment($message);
    }

    public function saveCheckoutComment ($observer)
    {
        $order = $observer->getOrder();
        $message = Mage::getSingleton('core/session')->getCheckoutComment(true);

        if (!$order || empty($message)) {
            return;
        }

        $commentModel = Mage::getModel('checkoutcomments/comment');
        $commentModel->setOrderId($order->getId())
            ->setComment($message)
            ->save();
    }
}

?>
3

The observer name of every observer has to be unique. So at least one of the save_checkout_comments tags has to go. I normally use the module name + event so in your case that would be

<controller_action_predispatch_checkout_onepage_saveOrder>
    <observers>
        <checkoutcomments_controller_action_predispatch_checkout_onepage_saveOrder>
        <class>checkoutcomments/observer</class>
        <method>prepareCheckoutComment</method>
        </checkoutcomments_controller_action_predispatch_checkout_onepage_saveOrder>
    </observers>
</controller_action_predispatch_checkout_onepage_saveOrder>

<sales_order_place_after>
    <observers>
        <checkoutcomments_sales_order_place_after>
        <class>checkoutcomments/observer</class>
        <method>saveCheckoutComment</method>
        </checkoutcomments_sales_order_place_after>
    </observers>
</sales_order_place_after>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.