Magento Add Error Message To Controller

Magento Add Error Message To Controller

October 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Add Error Message To Controller

Implementing Address Validation in Magento Prescription Checkout

As a senior developer working on e-commerce platforms like Magento, I often encounter challenges that demand creative solutions to meet specific business requirements. In this case, we have a pharmacy client who needs to ensure customers have a physical address on file before they can checkout with prescription medications.

The Challenge: Conditional Address Validation

The primary obstacle is implementing an error message and validation mechanism within the custom controller after the standard Magento checkout process. We need to check if any of the customer’s addresses contain a physical street address, and prevent further progress if none are found.

Identifying the Issue in Existing Code

Looking at the provided code snippet, we can see that the developer is on the right track by filtering the customer’s addresses using a regular expression. However, there are two key issues to address:

  1. The error message is being added during the boolean condition evaluation, which means it will always be displayed regardless of whether an address is found or not.
  2. There’s no actual validation happening before the user reaches this point in the checkout process. This leads to a confusing user experience.

A Better Approach: Early Validation and Error Handling

To solve these issues, we should move the error handling logic to the appropriate event observer in Magento. By using an checkout_controller_onepage_save_before or core_session_start_abort event, we can validate the customer’s addresses early in the checkout process.

Here’s a step-by-step guide to implementing this:

  1. Create a new module if you don’t have one already.
  2. In your config.xml file (inside etc/), define an observer for the checkout_controller_onepage_save_before event:
<events>
    <checkout_controller_onepage_save_before>
        <observers>
            <validate_customer_addresses>
                <type>singleton</type>
                <class>YourCompany_Module_Model_Checkout</class>
                <method>validateAddresses</method>
            </validate_customer_addresses>
        </observers>
    </checkout_controller_onepage_save_before>
</events>
  1. Create the ValidateAddresses method in your observer:
public function validateAddresses(Varien_Event_Observer $observer)
{
    $session = Mage::getSingleton('core/session');
    $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();

    $addresses = Mage::getModel('customer/address_collection')
        ->addFieldToFilter('parent_id', $customerId)
        ->load();

    if (count(array_filter($addresses, function ($address) {
            return !preg_match("(?i)^\\s*((P(OST)?.?\\s*(O(FF(ICE)?)?.?\\s+(B(IN|OX))?)|B(IN|OX))", $address->getStreet());
        }))) == 0) {

        Mage::throwException('Unfortunately we are required to have a physical address on file when shipping any controlled medications. Please note that although we are required to have a physical address on file, we still ship to PO Boxes. We sincerely apologize for the inconvenience.');
    }
}

This observer will throw an exception if no valid physical addresses are found before the checkout process is saved.

  1. In your template or controller, handle this exception and display the error message accordingly:
catch (Mage_Core_Exception $e) {
    Mage::getSingleton('core/session')->addError($e->getMessage());
    // Redirect to the address book page for the customer to update their addresses
}

Conclusion: Efficient Address Validation in Magento

By implementing this approach, we ensure that customers are validated early in the checkout process and redirected back to update their addresses when necessary. This provides a smoother user experience and reduces confusion during the prescription medication checkout flow.

Remember to always test these changes thoroughly in your development environment before deploying them to your live Magento site. For more detailed guides on Magento customization, you can visit Laravel Company’s Magento Development Services.

Feel free to reach out if you need further assistance or have any other questions about Magento development!

Checkout Extensions Themes Customers

Our network