2011-06-02 77 views
13

是否有一種簡單的方法可以將管理歷史記錄中發表評論的人的用戶名添加到訂單上的評論帖子中?添加用戶名以添加評論更改歷史記錄

- 編輯 -

問這將是我怎麼添加其他字段來註釋歷史模型,以便我可以覆蓋相應的模型和模板中插入數據到數據結構的另一種方式。

+0

回答你的第一個問題是沒有:) – 2011-06-02 20:30:55

回答

8

如果您想添加當前登錄的用戶名並按順序進行更改或評論訂單。您需要向magento添加一個屬性。

創建模塊說審計 應用程序的/ etc /模塊/ Namespace_Audit.xml

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Namespace_Audit> 
      <active>true</active> 
      <codePool>local</codePool> 
      <depends> 
       <Mage_Sales/> 
      </depends> 
     </Namespace_Audit> 
    </modules> 
</config> 

然後創建一個文件夾審覈你的命名空間和創建配置文件。的這一目的是重寫的核心類和改性方法延伸

應用程序/代碼/本地/命名空間/審計的/ etc/config.xml中

`<?xml version="1.0"?> 
<config> 
    <modules> 
     <Namespace_Audit> 
      <version>0.1.0</version> 
     </Namespace_Audit> 
    </modules> 
    <global> 
     <blocks> 
      <adminhtml> 
       <rewrite> 
        <sales_order_view_tab_history before="Mage_Adminhtml_Block">Namespace_Audit_Block_Sales_Order_View_Tab_History<sales_order_view_tab_history> 
       </rewrite> 
      </adminhtml> 
     </blocks>      
     <global> 
       <models> 
         <audit> 
           <class>Bigadda_Audit_Model</class> 
         </audit> 
       </models> 
     <resources>  
      <audit_setup> 
       <setup> 
        <module>Bigadda_Audit</module> 
       </setup> 
       <connection> 
        <use>core_setup</use> 
       </connection> 
      </audit_setup> 
      <audit_write> 
       <connection> 
        <use>core_write</use> 
       </connection> 
      </audit_write> 
      <audit_read> 
       <connection> 
        <use>core_read</use> 
       </connection> 
      </audit_read> 
     </resources> 
     </global> 
    </global> 
</config>` 

創建設置,以使在數據庫中的一個新的屬性 本地/命名空間/審計/ SQL/audit_setup/mysql4安裝-0.1.0.php

` 
<?php 
$installer = $this; 
$installer->startSetup(); 
$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$setup->addAttribute('order_status_history', 'track_user', array('type' => 'varchar')); 
$installer->endSetup(); 
` 

現在擴展現有的類。創建一個類文件History.php

命名空間/審計/座/銷售/ /查看/標籤/歷史

,並在

` 公共職能getFullHistory複製功能(){ $ order = $ this-> getOrder();

$history = array(); 
    foreach ($order->getAllStatusHistory() as $orderComment){ 
     $history[$orderComment->getEntityId()] = $this->_prepareHistoryItem(
      $orderComment->getStatusLabel(), 
      $orderComment->getIsCustomerNotified(), 
      $orderComment->getCreatedAtDate(), 
      $orderComment->getComment(), 
      $orderComment->getTrackUser(), 
      $orderComment->getTrackUserName() 
     ); 
    } 

    foreach ($order->getCreditmemosCollection() as $_memo){ 
     $history[$_memo->getEntityId()] = 
      $this->_prepareHistoryItem($this->__('Credit Memo #%s created', $_memo->getIncrementId()), 
       $_memo->getEmailSent(), $_memo->getCreatedAtDate()); 

     foreach ($_memo->getCommentsCollection() as $_comment){ 
      $history[$_comment->getEntityId()] = 
       $this->_prepareHistoryItem($this->__('Credit Memo #%s comment added', $_memo->getIncrementId()), 
        $_comment->getIsCustomerNotified(), $_comment->getCreatedAtDate(), $_comment->getComment(),$_comment->getTrackUser(),$_comment->getTrackUserName()); 
     } 
    } 

    foreach ($order->getShipmentsCollection() as $_shipment){ 
     $history[$_shipment->getEntityId()] = 
      $this->_prepareHistoryItem($this->__('Shipment #%s created', $_shipment->getIncrementId()), 
       $_shipment->getEmailSent(), $_shipment->getCreatedAtDate()); 

     foreach ($_shipment->getCommentsCollection() as $_comment){ 
      $history[$_comment->getEntityId()] = 
       $this->_prepareHistoryItem($this->__('Shipment #%s comment added', $_shipment->getIncrementId()), 
        $_comment->getIsCustomerNotified(), $_comment->getCreatedAtDate(), $_comment->getComment(),$_comment->getTrackUser(),$_comment->getTrackUserName()); 
     } 
    } 

    foreach ($order->getInvoiceCollection() as $_invoice){ 
     $history[$_invoice->getEntityId()] = 
      $this->_prepareHistoryItem($this->__('Invoice #%s created', $_invoice->getIncrementId()), 
       $_invoice->getEmailSent(), $_invoice->getCreatedAtDate()); 

     foreach ($_invoice->getCommentsCollection() as $_comment){ 
      $history[$_comment->getEntityId()] = 
       $this->_prepareHistoryItem($this->__('Invoice #%s comment added', $_invoice->getIncrementId()), 
        $_comment->getIsCustomerNotified(), $_comment->getCreatedAtDate(), $_comment->getComment(),$_comment->getTrackUser(),$_comment->getTrackUserName()); 
     } 
    } 

    foreach ($order->getTracksCollection() as $_track){ 
     $history[$_track->getEntityId()] = 
      $this->_prepareHistoryItem($this->__('Tracking number %s for %s assigned', $_track->getNumber(), $_track->getTitle()), 
       false, $_track->getCreatedAtDate()); 
    } 

    krsort($history); 
    return $history; 
}` 

protected function _prepareHistoryItem($label, $notified, $created, $comment = '' , $trackUser = '' , $trackUserName ='') 
    { 
     return array(
      'title'  => $label, 
      'notified' => $notified, 
      'track_user' => $trackUser, 
      'track_user_name' => $trackUserName, 
      'comment' => $comment, 
      'created_at' => $created    
     ); 
    } 

擴展類order.php並添加此方法來設置註釋以更新數據庫。 應用程序/代碼/本地/ myNameSpace對象/銷售/型號/ Order.php

public function addStatusHistoryComment($comment, $status = false) 
     { 
       if (false === $status) { 
         $status = $this->getStatus(); 
       } elseif (true === $status) { 
         $status = $this->getConfig()->getStateDefaultStatus($this->getState()); 
       } else { 
         $this->setStatus($status); 
       } 
       $UserInfo = Mage::getSingleton('admin/session')->getUser(); 
       $UserName=''; 
       $UserName=$UserInfo->getUsername(); 
       $history = Mage::getModel('sales/order_status_history') 
       ->setStatus($status) 
       ->setComment($comment) 
       ->setTrackUser($UserName); //added by vipul dadhich to add audits in the 
       $this->addStatusHistory($history); 
       return $history; 

     } 

最後更新PHTML文件。 應用程序/設計/ adminhtml /默認/缺省的/模板/銷售/訂單/查看/ history.phtml 將此代碼哪裏ü要顯示的用戶名

<?php if ($_item->getTrackUser()): ?> 
       <br/><?php echo "<b>Updated By (User) :- </b>".$this->htmlEscape($_item->getTrackUser(), array('b','br','strong','i','u')) ?> 
      <?php endif; ?> 

應用程序/設計/ adminhtml /默認/缺省的/ template/sales/order/view/tab/history.phtml

<?php if ($_comment = $this->getItemTrackUser($_item)): ?> 
        <br/><?php echo "<b>Updated By (User) :- </b>".$_comment ?> 
       <?php endif; ?> 

Thats All folks ..

VIPUL Dadhich

+1

感謝在這一個幫助。我不得不做一些調整,但你的代碼已經死了。 – Chris 2011-06-16 13:56:29

+1

我發現的一個大問題是Mage :: getSingleton('admin/session') - > getUser();在前端不可用,並導致我的正常結帳失敗。你碰到過這個嗎?你是如何克服它的? – Chris 2011-06-16 15:14:04

+1

我找到了解決方法,你需要在Model 上執行此操作。$ UserInfo = Mage :: getSingleton('admin/session') - > getUser(); \t \t \t如果($的UserInfo) \t \t \t \t $ USERNAME = $ UserInfo-> getUsername(); – Chris 2011-06-16 15:44:58

6

不同的看法通過觀察事件* sales_order_status_history_save_before *

定義在你的配置設置和觀察員:

<config> 
    <modules> 
     <Name_Module> 
      <version>0.0.1</version> 
     </Name_Module> 
    </modules> 
    <global> 
     <resources> 
      <module_setup> 
       <setup> 
        <module>Name_Module</module>      
       </setup> 
       <connection> 
        <use>core_setup</use> 
       </connection> 
      </module_setup> 
     </resources>  
     <events> 
      <sales_order_status_history_save_before> 
       <observers> 
        <sales_order_status_history_save_before_observer> 
         <type>singleton</type> 
         <class>Name_Module_Model_Observer</class> 
         <method>orderStatusHistorySaveBefore</method> 
        </sales_order_status_history_save_before_observer> 
       </observers> 
      </sales_order_status_history_save_before>  
     </events> 
    <!-- and so on -> 

在你module_setup文件app \代碼\本地\ Name \ Module \ sql \ module_setup \ install-0.0.1.php

$installer = $this; 
$installer->startSetup(); 
$table = $installer->getTable('sales/order_status_history'); 
$installer->getConnection() 
    ->addColumn($table, 'username', array(
     'type'  => Varien_Db_Ddl_Table::TYPE_TEXT, 
     'length' => 40, 
     'nullable' => true, 
     'comment' => 'Admin user name' 
    )); 
$installer->getConnection() 
    ->addColumn($table, 'userrole', array(
     'type'  => Varien_Db_Ddl_Table::TYPE_TEXT, 
     'length' => 50, 
     'nullable' => true, 
     'comment' => 'Admin user role' 
    ));  
$installer->endSetup(); 

然後在Name_Module_Model_Observer:

public function orderStatusHistorySaveBefore($observer) 
{ 
    $session = Mage::getSingleton('admin/session'); 
    if ($session->isLoggedIn()) { //only for login admin user 
     $user = $session->getUser(); 
     $history = $observer->getEvent()->getStatusHistory(); 
     if (!$history->getId()) { //only for new entry 
      $history->setData('username', $user->getUsername()); 
      $role = $user->getRole(); //if you have the column userrole 
      $history->setData('userrole', $role->getRoleName()); //you can save it too 
     }    
    } 
} 
+0

好,簡單和乾淨的方式。謝謝哥們。 – 2015-06-02 12:50:09