djames
djames

Reputation: 227

Magento Add Error Message To Controller

I work for a Pharmacy and we are trying to implement it so that when a customer checks out a prescription medication, they have to have a physical address on file. We have the standard Magento checkout controller, and then a custom controller loads after they go through that page if they have a prescription medication in their cart. My question is how can I make it so if they have no physical address on file, the page will produce an error message and will not allow them to continue unless adding a physical address to their user account.

Here is the code I have thus far, in the controller for the page:

    public function checkAddressOnFile()
    {
        return (bool) count(array_filter(Mage::getSingleton('customer/session')->getCustomer()->getAddresses(), function($address) {
            return !preg_match("(?i)^\\s*((P(OST)?.?\\s*(O(FF(ICE)?)?)?.?\\s+(B(IN|OX))?)|B(IN|OX)", $address);
        }));
            Mage::getSingleton('core/session')->addError('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.' . $e->getMessage());
    }

Upvotes: 1

Views: 2192

Answers (1)

PRashant PUrohit
PRashant PUrohit

Reputation: 390

public function checkAddressOnFile()
{

    return (bool) count(array_filter(Mage::getSingleton('customer/session')->getCustomer()->getAddresses(), function($address) {

        Mage::getSingleton('core/session')
          ->addError('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.' . $e->getMessage());

        return !preg_match("(?i)^\\s*((P(OST)?.?\\s*(O(FF(ICE)?)?)?.?\\s+(B(IN|OX))?)|B(IN|OX)", $address);
    }));

}

For Notice

Mage::getSingleton('core/session')->addNotice('Notice Text...');

For success message

Mage::getSingleton('core/session')->addSuccess('Success Text...');

For a error message

Mage::getSingleton('core/session')->addError('Error Text...');

Upvotes: 2

Related Questions