我想知道是否可以在自定义消息中启用HTML链接(即:错误消息)。
这是我的例子: 我已经覆盖了我对Mage_CatalogInventory_Model_Stock_Item / Item.php的需求
函数checkQuoteItemQty:
if (!$this->checkQty($summaryQty) || !$this->checkQty($qty)) {
//$message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available.', $this->getProductName());
$message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available (max:%s).', $this->getProductName(), ($this->getQty() * 1));
$cat_id = $this->getProduct()->getCategoryIds();
if($cat_id){
$url = Mage::getModel('catalog/category')->load($cat_id[0])->getUrl();
$message .= Mage::helper('cataloginventory')->__('You might be interested in <a href="%s">those products</a>.', $url);
}
$result->setHasError(true)
->setMessage($message)
->setQuoteMessage($message)
->setQuoteMessageIndex('qty');
return $result;
}
但我在$ message中创建的HTML链接不可点击并被视为文本(因为我猜的是翻译...)。 是否有可能改变这种行为?
问候。
答案 0 :(得分:2)
对于那些想知道的人,我不得不重写Mage_Core_Block_Messages,第249行:
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()) : html_entity_decode($message->getText());
$html.= '</' . $this->_messagesContentWrapperTagName . '>';
$html.= '</' . $this->_messagesSecondLevelTagName . '>';
}
$html .= '</' . $this->_messagesFirstLevelTagName . '>';
$html .= '</' . $this->_messagesSecondLevelTagName . '>';
}
}
if ( $html) {
$html .= '</' . $this->_messagesFirstLevelTagName . '>';
}
return $html;
}
在此行添加html_entity_decode:
$html.= ($this->_escapeMessageFlag) ? $this->escapeHtml($message->getText()) : html_entity_decode($message->getText());
如果您找到更好的解决方案,请告诉我。
答案 1 :(得分:0)
我不知道为什么,但在我的情况下,通过调用没有“消息”的过时方法解决了链接的问题。
我没有使用addNoticeMessage
,而是更改为addNotice
,并显示了链接。
就我而言:
$noticeMsg = __('You must be <a href="%1">logged in</a> or <a href="%2">registered</a> to purchase these products.',
$this->_storeManager->getStore()->getUrl('customer/account/login'),
$this->_storeManager->getStore()->getUrl('customer/account/create')
);
$this->_messageManager->addNotice($noticeMsg);