Add markup to out of checkout/cart stock message
-
12-12-2019 - |
Question
I have copied /app/code/Core/Mage/CatalogInventory/Model/Stock/Item.php
to /app/code/local/Mage/CatalogInventory/Model/Stock/Item.php
. At line (aprox) 580 the following code exists
if (!$this->getIsInStock()) {
$result->setHasError(true)
->setMessage(Mage::helper('cataloginventory')->__('This product is currently out of stock.'))
->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products are currently out of stock.'))
->setQuoteMessageIndex('stock');
$result->setItemUseOldQty(true);
return $result;
}
I need to edit the text inside the setQuoteMessage
function to include <a href="/shop">continue shopping </a>
. There seems to be some kind of escape html function hitting it between it leaving this file and it getting to the view templates because no matter what I try doing the browser just displays the a tag
as regular text.
For session messages I've found this option where you just set $session->setEscapeMessages(false);
. Is there any kind of similar param I can add on the $result
which will do the same.
I have tried using the session messages instead but it means that I lose functionality in terms of product specific messages and message position based on the product that is out of stock. So adding a flag to the original code and the updated text would be my absolute preferred solution.
Solution 2
Using a bit of a hack and the link @tecjam provided I've got a dirty but working solution.
In /app/code/local/Mage/CatalogInventory/Model/Stock/Item.php
inside the function checkQuoteItemQty
at the parts where qty messages get set I set a session variable;
Mage::getSingleton('core/session')->setCartCheckoutErr(true);
Then having copied Mage_Core_Block_Messages
to /app/code/local/Mage/Core/Block/Messages.php
I went to the function getGroupedHtml()
at line 229 and modified it like so;
public function getGroupedHtml()
{
$types = array(
Mage_Core_Model_Message::ERROR,
Mage_Core_Model_Message::WARNING,
Mage_Core_Model_Message::NOTICE,
Mage_Core_Model_Message::SUCCESS
);
$html = '';
foreach ($types as $type) {
if ( $messages = $this->getMessages($type) ) {
if ( !$html ) {
$html .= '<' . $this->_messagesFirstLevelTagName . ' class="messages">';
}
$html .= '<' . $this->_messagesSecondLevelTagName . ' class="' . $type . '-msg">';
$html .= '<' . $this->_messagesFirstLevelTagName . '>';
foreach ( $messages as $message ) {
$html.= '<' . $this->_messagesSecondLevelTagName . '>';
$html.= '<' . $this->_messagesContentWrapperTagName . '>';
$html.= ($this->_escapeMessageFlag) ? $this->escapeHtml($message->getText()) : $message->getText();
================================**MY CHANGES**============================================
if(Mage::getSingleton('core/session')->getCartCheckoutErr()){
$html.= ' Email ' . Mage::getStoreConfig('trans_email/ident_sales/email') .' or <a href="/shop">continue shopping </a>';
Mage::getSingleton('core/session')->unsCartCheckoutErr();
}
================================**MY CHANGES**============================================
$html.= '</' . $this->_messagesContentWrapperTagName . '>';
$html.= '</' . $this->_messagesSecondLevelTagName . '>';
}
$html .= '</' . $this->_messagesFirstLevelTagName . '>';
$html .= '</' . $this->_messagesSecondLevelTagName . '>';
}
}
if ( $html) {
$html .= '</' . $this->_messagesFirstLevelTagName . '>';
}
return $html;
}
It was important to add in some logic to only add the markup in certain situations because obviously I don't want it added every time an error is rendered. However this may not be required for everyone and in such cases a cleaner solution could be more easily attained.
OTHER TIPS
EDIT:
The answer can be found here:
Magento 1.9 CE - Enable HTML links in error/custom messages
The rest of this answer can be disgarded.
An example on how this can be done can be found here:
/app/design/frontend/base/default/template/checkout/success.phtml
<p><?php echo $this->__('Your order # is: %s.', sprintf('<a href="%s">%s</a>', $this->escapeHtml($this->getViewOrderUrl()), $this->escapeHtml($this->getOrderId()))) ?></p>
-- edit:
So you can use 2 functions like above, or hardcode it:
->setQuoteMessage(Mage::helper('cataloginventory')->__('Some of the products are currently out of stock. %s', sprintf('<a href="%s">%s</a>', '/shop', 'continue shopping')))
The functions are defined here: /app/code/core/Mage/Checkout/Block/Onepage/Success.php:
/**
* Retrieve identifier of created order
*
* @return string
* @deprecated after 1.4.0.1
*/
public function getOrderId()
{
return $this->_getData('order_id');
}
/**
* Get url for view order details
*
* @return string
* @deprecated after 1.4.0.1
*/
public function getViewOrderUrl()
{
return $this->_getData('view_order_id');
}