2010-06-16 5 views
6

Magento에서 성공 메시지를 검색하려면 어떻게합니까?Magento에서 성공 메시지를 검색하려면 어떻게합니까?

Array 
(
    [core] => Array 
     (
      [_session_validator_data] => Array 
       (
        [remote_addr] => 192.168.151.102 
        [http_via] => 
        [http_x_forwarded_for] => 
        [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4 
       ) 

      [session_hosts] => Array 
       (
        [technova2] => 1 
       ) 

      [messages] => Mage_Core_Model_Message_Collection Object 
       (
        [_messages:protected] => Array 
         (
         ) 

        [_lastAddedMessage:protected] => Mage_Core_Model_Message_Success Object 
         (
          [_type:protected] => success 
          [_code:protected] => Your review has been accepted for moderation 
          [_class:protected] => 
          [_method:protected] => 
          [_identifier:protected] => 
          [_isSticky:protected] => 
         ) 

       ) 

      [just_voted_poll] => 
      [visitor_data] => Array 
       (
        [] => 
        [server_addr] => -1062692990 
        [remote_addr] => -1062693018 
        [http_secure] => 
        [http_host] => technova2 
        [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4 
        [http_accept_language] => en-US,en;q=0.8 
        [http_accept_charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.3 
        [request_uri] => /~rahuls/sextoys/index.php/review/product/list/id/169/ 
        [session_id] => 21bq2vtkup5m1gtghknlu1tit42c6dup 
        [http_referer] => http://technova2/~rahuls/sextoys/index.php/review/product/list/id/169/ 
        [first_visit_at] => 2010-06-16 05:49:56 
        [is_new_visitor] => 
        [last_visit_at] => 2010-06-16 06:00:00 
        [visitor_id] => 935 
        [last_url_id] => 23558 
       ) 

      [last_url] => http://technova2/~rahuls/sextoys/index.php/review/product/list/id/169/ 
     )  
) 

리뷰를 게시 한 후 "귀하의 검토가 검토를 위해 승인되었습니다."라는 메시지가 표시됩니다. 그것은 $ _SESSION 배열에 나타나지만 그것을 가져 오는 방법은 무엇입니까? 도와주세요. 미리 감사드립니다.

+0

당신이이 질문을 표시 시겠어요 해결과 같이하여 보낸 메시지의 당신의 필요 유형을 검색 할 수 있습니다? 감사! – Nitroware

답변

3

메시지가 core 하위 배열에 저장되었으므로 Magento 핵심 메시지 블록을 사용하여 메시지를 검색합니다. 레이아웃, 당신은 (에서 page.xml에서)이 줄을 볼 수 있어야합니다 :

<block type="core/messages" name="global_messages" as="global_messages"/> 

이 페이지는 메시지 블록을 호출하고 그 핵심 배열에서 메시지를 검색하는 것을 의미한다.

<?php echo $this->getChildHtml('global_messages') ?> 

이 실제로 세션에서 모든 메시지를 일반 메시지 블록을 메아리 : 그런 다음, 레이아웃, 당신은 실제로 출력을 호출하는 라인을 볼 수 있어야합니다. . 당신이 그 블록을 찾을 수없는 경우에 추가 다른 맥락에서 메시지를받을 필요가있는 경우 (이 다른 사이트의 작동을 방해 할 수 있음)을하는 PHTML 파일이 시도 : 도움이

<?php print $this->getLayout()->createBlock('core/messages')->toHtml(); ?> 

희망을!

덕분에, 조

2

은 내가 성공 메시지하지만 성공에 대한 내 코드에 두 줄 아래의 사용에서 page.xml

에서 아래 블록을 발견했다.

getChildHtml ('global_messages')?> getLayout() -> createBlock ('core/messages') -> toHtml(); ?>
5

요청하신 내용이 Magento에 이미 존재하는 것으로 보입니다. 사용자가 제품에 대한 리뷰를 게시하면 app/code/core/Mage/Review/controllers/ProductController.php 줄 188 (Magento 1.4)에 명시된대로 "검토가 중재를 위해 승인되었습니다."라는 메시지가 기본적으로 표시됩니다. 당신은 단지 성공 메시지의 경우 예를 들어, 사용) 메시지 (공지, 성공, 오류, 경고를 표시 할 경우 0.1)

어쨌든, :

<?php 
$message = $this->__('Your success message here'); 
Mage::getSingleton('core/session')->addSuccess($message); 
?> 

이 메시지는 세션에 저장됩니다 페이지의 템플릿 파일에 Magento와 함께 제공되는 모든 기본 phtml 페이지의 경우 인 $ this-> getMessagesBlock() -> getGroupedHtml() 코드가있는 한 자동으로 프론트 엔드에 나타납니다. 그래서 당신은 정말로 신경 쓰지 않아도됩니다. 물론

, 위의 예에서, 당신은 정보를 표시하도록 희망의 종류의 따라 addError($message) 또는 addWarning($message) 또는 addNotice($message)에 의해

addSuccess($message) 

을 변경할 수 있습니다.

6
$messages = Mage::getSingleton('core/session')->getMessages(true); 
foreach($messages->getItems() as $message) 
{ 
    // Do something 
    $message->getText(); 
} 
+0

당신의 필요에 맞게 모델을 변경하는 것을 잊지 마세요. Mage :: getSingleton ('customer/session')' – tecmec

9

모든 답변을 조합 한 것입니다.

//A Success Message 
    Mage::getSingleton('checkout/session')->addSuccess("Your cart has been updated successfully!"); 

    //A Error Message 
    Mage::getSingleton('checkout/session')->addError("Your cart has been updated successfully!"); 

    //A Info Message (See link below) 
    Mage::getSingleton('checkout/session')->addNotice("This is just a FYI message..."); 

    //These two lines are required to get it to work 
    session_write_close(); //THIS LINE IS VERY IMPORTANT! 
    $this->_redirect('checkout/cart'); 

신용 인해 :

http://www.magentocommerce.com/boards/viewthread/40324/

http://www.deepcodeonline.com/blog/magento/how-to-display-error-success-and-notice-messages-in-magento/

9

다음 코드 (내가 대답을 게시 경우) 이것은 단지 어떤 블록에 대한에서 나를 위해 작동 나를위한 작품 :

  1. 세트 메시지 컨트롤러 : 당신이 메시지를 검색 할 컨트롤러

    Mage::getSingleton('customer/session') 
        ->addSuccess(Mage::helper('mymodule')->__('Data saved.')); 
    
  2. 초기화 메시지 :

    $this->loadLayout(); 
    $this->_initLayoutMessages('customer/session'); 
    $this->_initLayoutMessages('catalog/session'); 
    $this->renderLayout();` 
    
  3. 이 템플릿에서 메시지를 검색을 (.phtml) 파일 :

    echo $this->getMessagesBlock()->getGroupedHtml(); 
    
  4. 그런 다음

    다음과 같이 세션에서 메시지를 검색했다 : false로부터 메시지 모음을 취소하지 않습니다 퍼팅

    $messages = Mage::getSingleton('catalog/session')->getMessages(false); 
    

+0

$ this-> initLayoutMessages ('checkout/session'); 내 컨트롤러에서 기본 "X를 꺼내려고하면 쇼핑 카트에 추가되었습니다." 메시지. 기본적으로 메시지가 저장되는 위치를 찾고 initLayoutMessages 호출에 저장합니다. – jay

+0

이것이 Magento 메시지가 작동하는 방법을 찾을 수있는 가장 간결한 설명입니다. 이것을 찾는 다른 사람들을 위해, 다른 일반적인 메시지 유형은 checkout/session과 core/session을 포함합니다. 확장 기능은 임의의 위치에 메시지를 넣는 경향이 있으므로 때로는 모든 메시지를 초기화하는 것이 더 쉬워서 원하는 메시지를 얻을 때까지 하나씩 차례로 꺼내십시오. – Ucinorn

3

는 카탈로그 세션에서 성공 메시지를 검색한다고 가정 세션. 검색 후 모든 메시지를 지우려면 true을 대신 사용하십시오.

$messagesMage_Core_Model_Message_Collection 유형입니다.

$successMessages = $messages->getItemsByType(Mage_Core_Model_Message::SUCCESS); 

당신은에 의해 해당 메시지를 통해 루프 수 : 솔루션과 같은 답변을 선택하여

foreach ($successMessages as $message) { 
    //do whatever you like 
} 
+0

작동. 올바른 세션 객체를 얻었 으면'('customer/session')'이 필요합니다. – Justin

관련 문제