在Magento销售订单视图中显示订单注释作者的姓名

时间:2013-05-16 09:22:40

标签: php magento magento-1.7

我创建了一个自定义模块,允许在Magento前端提交订单评论 Sales_Guest_View

我可以在前端显示评论作者的姓名,但我还需要在Sales_Order_View的“管理”面板中显示该名称。

原因是在Magento Admin中跟踪订单评论等。

以下是表单的前端控制器,显示作者姓名

<?php

require_once Mage::getModuleDir('Controller', 'Mage_Sales') . DS . 'Controller' . DS . 'Abstract.php';

class Tamedo_FrontendOrderMessage_FormController extends Mage_Sales_Controller_Abstract {

    protected $_guest_cookieName  = 'guest-view';
    protected $_guest_lifeTime    = 600;

    public function orderAction() {
        if(Mage::getSingleton('customer/session')->isLoggedIn()) {
            $this->_loadValidOrder();
        } else {
            Mage::helper('sales/guest')->loadValidOrder();
        }
        $order = Mage::registry('current_order');
        $orderId = $order->getId();
        if(empty($orderId)) {
            if (Mage::getSingleton('customer/session')->isLoggedIn()) {
                $this->_redirect('sales/order/history');
            } else {
                $this->_redirect('sales/guest/form');
            }
        } else {
            $data = $this->getRequest()->getParams();
            if (!empty($data['history']['comment']))
                $append = " Posted By: ".$username.""; {
                $order->addStatusHistoryComment($data['history']['comment'], false)
                        ->setIsVisibleOnFront(true)
                        ->setIsCustomerNotified(false);
                $order->save();
            }
            if (Mage::getSingleton('customer/session')->isLoggedIn()) {
                $this->_redirect('sales/order/view', array('order_id' => $order->getId()));
            } else {
                $toCookie = base64_encode($order->getProtectCode());
                Mage::getSingleton('core/cookie')->set($this->_guest_cookieName, $toCookie, $this->_guest_lifeTime, '/');
                $this->_redirect('sales/guest/view');
            }
        }
    }

}

管理员控制器仅显示提交评论的管理员的姓名

<?php
/**
 * Tamedo
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @category    Atwix
 * @package     Atwix_OrderComment
 * @author      Atwix Core Team
 * @copyright   Copyright (c) 2012 Atwix (http://www.atwix.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
require_once 'Mage/Adminhtml/controllers/Sales/OrderController.php';
class Tamedo_OrderComment_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
{
    /**
     * Add order comment action
     */
    public function addCommentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $response = false;
                 //getting username
                $user = Mage::getSingleton('admin/session');
                $username = $user->getUser()->getUsername();
                $append = " posted by: ".$username."";
                $data = $this->getRequest()->getPost('history');
                $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
                $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;


                //appending username with markup to comment
                $order->addStatusHistoryComment($data['comment'].$append, $data['status'])
                    ->setIsVisibleOnFront($visible)
                    ->setIsCustomerNotified($notify);

                $comment = trim(strip_tags($data['comment']));

                $order->save();
                $order->sendOrderUpdateEmail($notify, $comment);

                $this->loadLayout('empty');
                $this->renderLayout();
            }
            catch (Mage_Core_Exception $e) {
                $response = array(
                    'error'     => true,
                    'message'   => $e->getMessage(),
                );
            }
            catch (Exception $e) {
                $response = array(
                    'error'     => true,
                    'message'   => $this->__('Cannot add order history.')
                );
            }
            if (is_array($response)) {
                $response = Mage::helper('core')->jsonEncode($response);
                $this->getResponse()->setBody($response);
            }
        }
    }
}

有谁知道如何在后面或我需要修改代码的位置包含前端作者姓名。我尝试过,但一直导致错误..

1 个答案:

答案 0 :(得分:1)

我不是100%确定您要做什么,但您可以尝试使用订单结算或运输名称作为用户名或获取当前登录用户信息。

从会话中获取客户名称

$username = Mage::getSingleton('customer/session')->getCustomer()->getName()

从订单中获取客户信息

$username = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname()

然后将您的代码更新为

.....
if (!empty($data['history']['comment']))
    $comment = $data['history']['comment'] . " Posted By: {$username}."; {
    $order->addStatusHistoryComment($comment, false)
          ->setIsVisibleOnFront(true)
          ->setIsCustomerNotified(false);
    $order->save();
 }