Magento – My Magento store Warnings and Error messages for users are displaying on wrong pages

magento-1.7messagessession

I've got a magento store that is working perfectly fine. But the only issue is that Error messages are not displaying where they suppose to be displayed.

For example the message displayed on the screenshot was supposed to be displayed on the onestepcheckout. But instead it is displayed on the homepage.

the main message that i'm concern with is the vat id error message:

on core i have found the public function of one of the messages that i want to place on a right page. currently the message "The VAT ID entered (%s) is not a valid VAT ID" is displayed on a wrong page. it suppose to display on onestepcheckout page:

public function getVatValidationUserMessage($customerAddress, $customerGroupAutoAssignDisabled, $validationResult)
{
    $message = '';
    $isError = true;
    $customerVatClass = $this->getCustomerVatClass($customerAddress->getCountryId(), $validationResult);
    $groupAutoAssignDisabled = Mage::getStoreConfigFlag(self::XML_PATH_CUSTOMER_VIV_GROUP_AUTO_ASSIGN);

    $willChargeTaxMessage    = $this->__('You will be charged tax.');
    $willNotChargeTaxMessage = $this->__('You will not be charged tax.');

    if ($validationResult->getIsValid()) {
        $message = $this->__('Your VAT ID was successfully validated.');
        $isError = false;

        if (!$groupAutoAssignDisabled && !$customerGroupAutoAssignDisabled) {
            $message .= ' ' . ($customerVatClass == self::VAT_CLASS_DOMESTIC
                ? $willChargeTaxMessage
                : $willNotChargeTaxMessage);
        }
    } else if ($validationResult->getRequestSuccess()) {
        $message = sprintf(
            $this->__('The VAT ID entered (%s) is not a valid VAT ID.') . ' ',
            $this->escapeHtml($customerAddress->getVatId())
        );
        if (!$groupAutoAssignDisabled && !$customerGroupAutoAssignDisabled) {
            $message .= $willChargeTaxMessage;
        }
    }
    else {
        $contactUsMessage = sprintf($this->__('If you believe this is an error, please contact us at %s'),
            Mage::getStoreConfig(self::XML_PATH_SUPPORT_EMAIL));

        $message = $this->__('Your Tax ID cannot be validated.') . ' '
            . (!$groupAutoAssignDisabled && !$customerGroupAutoAssignDisabled
                ? $willChargeTaxMessage . ' ' : '')
            . $contactUsMessage;
    }

    $validationMessageEnvelope = new Varien_Object();
    $validationMessageEnvelope->setMessage($message);
    $validationMessageEnvelope->setIsError($isError);

    return $validationMessageEnvelope;
}

Best Answer

The message that you have noted in your question is dealt with on the customer_address_save_after event. Once the function as been called the message is simply added to the customer session as either a success or an error message.

if (!$validationMessage->getIsError()) {
    Mage::getSingleton('customer/session')->addSuccess($validationMessage->getMessage());
} else {
    Mage::getSingleton('customer/session')->addError($validationMessage->getMessage());
}

Now these customer session messages can appear on pages that has one of the following blocks on it.

<block type="core/messages" name="global_messages" as="global_messages"/>
<block type="core/messages" name="messages" as="messages"/>

So if the user saves an address in the checkout the message is not guaranteed to display on the checkout page, as the complete page is not reloaded only the next section of the one page checkout. So the next page a user sees that could contain these messages could be the homepage.

Related Topic