【问题标题】:Magento Order History Comments: Modify DateMagento 订单历史评论:修改日期
【发布时间】:2013-10-03 23:29:52
【问题描述】:

我正在创建一个脚本以在 Magento 中以编程方式添加订单。我需要帮助来更改评论历史中条目的日期(报价、发票、运输等)。我可以操纵订单本身的日期 (setCreatedAt) 并且与订单创建相关的一些 cmets 是正确的(例如“2008 年 9 月 29 日上午 8:59:25 |待处理的客户通知不适用”),但我当我使用 addStatusHistoryComment 时无法更改评论的日期...

这是我的代码的 sn-p:

try {
if(!$order->canInvoice()) {
  Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
  Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without   products.'));
}

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->setCreatedAt('2008-09-23 13:05:20');
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(true);
$invoice->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
  ->addObject($invoice)
-  >addObject($invoice->getOrder());

$transactionSave->save();
//END Handle Invoice

//START Handle Shipment
$shipment = $order->prepareShipment();
$shipment->setCreatedAt('2008-09-23 14:20:10');
$shipment->register();
$order->setIsInProcess(true);
$order->addStatusHistoryComment('Shipping message goes here...', true);
$shipment->setEmailSent(true);
$transactionSave = Mage::getModel('core/resource_transaction')
  ->addObject($shipment)
  ->addObject($shipment->getOrder())
  ->save();
$track = Mage::getModel('sales/order_shipment_track')
  ->setShipment($shipment)
  ->setData('title', 'Some tracking no.')
  ->setData('number', '111222333444')
  ->setData('carrier_code', 'fedex') //custom, fedex, ups, usps, dhl
  ->setData('order_id', $shipment->getData('order_id'))
  ->save();
//END Handle Shipment
}
catch (Mage_Core_Exception $ex) {
  echo "Problem creating order invoice and/or shipment: ".$ex."\n";
}

评论历史截图: !http://www.etechparts.us/media/downloadable/files/links/CommentsHistory_Magento.png

提前致谢。

【问题讨论】:

    标签: magento


    【解决方案1】:

    如果我正确理解您的问题,您只需这样做:

    $comments = $order->getStatusHistoryCollection(true);
    

    $comments 现在包含所有状态历史 cmets 的集合,您可以使用任何您喜欢的标准循环它们。

    foreach ($comments as $c) {
        if ( /* some stuff */ ) {
            $c->setData('created_at',$new_date)->save();
        }
    }
    

    【讨论】:

    • 比我的解决方案更好的地方,喜欢方法描述“在此处输入描述...”:)
    • 是的,在 docs.magentocommerce.com 上查找并随后意识到您希望存在但未列出的方法很有趣......存在。作为一个粗略的指南很好,因为你不能相信它。他们真好。
    【解决方案2】:

    所以这是未经测试的,但应该可以工作:

    您需要基于addStatusHistoryComment 创建一个新方法: /app/code/core/Mage/Sales/Model/Order.php

    /*
     * Add a comment to order
     * Different or default status may be specified
     *
     * @param string $comment
     * @param string $status
     * @return Mage_Sales_Model_Order_Status_History
     */
    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);
        }
        $history = Mage::getModel('sales/order_status_history')
            ->setStatus($status)
            ->setComment($comment)
            ->setEntityName($this->_historyEntityName);
            ->setCreatedAt('2008-09-23 14:20:10'); //I added this line
        $this->addStatusHistory($history);
        return $history;
    }
    

    显然你要么需要重写这个方法,要么重构代码来做同样的事情。

    【讨论】:

    • 谢谢,输入。我已经尝试过修改现有的方法(只是在我麻烦扩展源代码之前看看它是否可行),但它没有用......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多