Reputation: 10898
Is it possible to overwrite a error/success message generated by the magento system?
For example if we add a product the success message is "Laptop is added to your cart..!", what if i would like to add my client's name like "Josh you had added Laptop to your cart"
Thanks, Balan
Upvotes: 1
Views: 5111
Reputation: 467
I've written a helper function that allows you to search for a specific message with an option to remove if found. It's a bit disappointing this isn't available in core..
/**
* Searches messages for @param $string.
* Will remove the message if $remove is true.
*
* @param string $string
* @param boolean $remove: false
* @param string $which: core/session
* @return true|false, found|not found
* @access public
*/
public function message_search( $string, $remove = false, $which = 'core/session' ) {
$found = false;
$messages = Mage::getSingleton( $which )->getMessages();
foreach( $messages->getItems() as $message )
if( stristr( $message->getText(), $string ) ) {
$found = true;
if( $remove ) $message->setIdentifier( 'this_message_will_be_removed' );
}
if( $remove ) $messages->deleteMessageByIdentifier( 'this_message_will_be_removed' );
return $found;
}
Upvotes: 4
Reputation: 27119
It's easy enough to add your custom message to the stack when an item is added. Add an event listener for checkout_cart_add_product_complete
which does this:
public function observeAddToCart($observer) {
$product = $observer->getEvent()->getProduct(); // you may need to play with this
$session = Mage::getSingleton("checkout/session")->addSuccess($message);
$message = Mage::helper("yourmodule")->__('%s, you added %s to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
$session->addSuccess($message);
}
That leaves the problem of removing the old message. The closest that I see right now is that you can clear all other messages on command by retrieving them. So you could clear the Magento-set message afterwards like so:
Mage::getSingleton("checkout/session")->getMessages(true);
You would need to do this after Magento's message has been added, however. Hope that gives you a start!
Thanks, Joe
Upvotes: 4