添加用户名以添加评论更改历史记录

问题描述:

是否有一种简单的方法可以将管理历史记录中发表评论的人的用户名添加到订单上的评论帖子中?添加用户名以添加评论更改历史记录

- 编辑 -

问这将是我怎么添加其他字段来注释历史模型,以便我可以覆盖相应的模型和模板中插入数据到数据结构的另一种方式。

+0

回答你的第一个问题是没有:) – 2011-06-02 20:30:55

如果您想添加当前登录的用户名并按顺序进行更改或评论订单。您需要向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

不同的看法通过观察事件* 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