2014-09-03 72 views
0

我想知道是否可以在自定义消息中启用HTML链接(即:错误消息)。Magento - 在错误/自定义消息中启用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; 
} 

但我在$消息创建的HTML链接是不可点击并被认为是文字(因为我猜...译文)。 是否有可能改变这种行为?

问候。

回答

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()); 

就让如果你找到一个我知道解决方案更好

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);