2014-09-03 56 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);