以编程方式为magento中已完成的订单发送发货电子邮件

时间:2013-03-18 06:02:52

标签: magento

我正在使用管理员端的magento默认发货。 所以它的工作正常,并完美地向客户发送电子邮件。

我想创建一个脚本,可以发送电子邮件,其中包含magento中所有已完成订单的货件详细信息。这仅适用于通过CSV传输的某些订单。

当我们使用处理订单的order_id时,我的脚本工作正常,但它不适用于已完成的订单。而不是在电子邮件中发送订单商品详情 请建议我任何解决方案..

非常感谢。

我使用以下脚本执行此操作:

function completeShipment($orderIncrementId,$shipmentTrackingNumber,$shipmentCarrierCode){
    $shipmentCarrierTitle = $shipmentCarrierCode;
    $customerEmailComments = '';
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    if (!$order->getId()) {
        Mage::throwException("Order does not exist, for the Shipment process to complete");
    }
    try {
        $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(_getItemQtys($order));

        $arrTracking = array(
                             'carrier_code' => isset($shipmentCarrierCode) ? $shipmentCarrierCode : $order->getShippingCarrier()->getCarrierCode(),
                             'title' => isset($shipmentCarrierTitle) ? $shipmentCarrierTitle : $order->getShippingCarrier()->getConfigData('title'),
                             'number' => $shipmentTrackingNumber,
                             );
        $track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
        $shipment->addTrack($track);
        $shipment->register();
        _saveShipment($shipment, $order, $customerEmailComments);
    }catch (Exception $e) {
        throw $e;
    }
    return $save;
}
function _getItemQtys(Mage_Sales_Model_Order $order){
    $qty = array();
    foreach ($order->getAllItems() as $_eachItem) {
        if ($_eachItem->getParentItemId()) {
            $qty[$_eachItem->getParentItemId()] = $_eachItem->getQtyOrdered();
        } else {
            $qty[$_eachItem->getId()] = $_eachItem->getQtyOrdered();
        }
    }
    return $qty;
}

function _saveShipment(Mage_Sales_Model_Order_Shipment $shipment, Mage_Sales_Model_Order $order, $customerEmailComments = ''){
    $shipment->getOrder()->setIsInProcess(true);

    $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($shipment)->addObject($order)->save();
    //$emailSentStatus = $shipment->getData('email_sent');

    $ship_data = $shipment->getOrder()->getData();
    $customerEmail = $ship_data['customer_email'];
    $emailSentStatus = $ship_data['email_sent'];

    if (!is_null($customerEmail)) {
        $shipment->sendEmail(true, $customerEmailComments);
        $shipment->setEmailSent(true);
    }
    return $this;
}







Thanks a lot dagfr...! the below code works for me to get done my task:


 function completeShipment($orderIncrementId,$shipmentIncreamentId){
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    $ship_data = $order->getData();
    $customerEmail = $ship_data['customer_email'];

    $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncreamentId);

    $customerEmailComments = '';
    $sent = 0;
    if (!is_null($customerEmail)) {
        $sent = $shipment->sendEmail(true, $customerEmailComments);
        $shipment->setEmailSent(true);
    }
    return $sent;
}

2 个答案:

答案 0 :(得分:2)

您的脚本会尝试创建货件,然后发送电子邮件。

对于已完成的订单,已经创建了货件,您无法再次创建,因此您的try在发送电子邮件之前就失败了。

就在之前:

try {
    $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(_getItemQtys($order));

检查订单状态,如果完成,只需要发货并发送邮件

  $shipment->sendEmail(true, $customerEmailComments);
  $shipment->setEmailSent(true);

否则你可以执行你的脚本。

答案 1 :(得分:0)

我使用order_shipment_api而不是service_order模型找到了成功。

如果您想在发货电子邮件中包含跟踪号,请务必先添加跟踪,然后使用Mage::getModel('sales/order_shipment_api')->sendInfo($shipmentIncrementId)

代码段:

        $shipmentApi = Mage::getModel('sales/order_shipment_api');

        //pass false for email, unless you want Magento to send the shipment email without any tracking info
        //could also be written as $shipmentIncrementId = $shipmentApi->create($order->getIncrementId());
        $shipmentIncrementId = $shipmentApi->create($order->getIncrementId(), array(), '' , false, 0); 

        //add tracking info ($shippingCarrier is case sensitive)
        $shipmentApi->addTrack($shipmentIncrementId, $shippingCarrier, $shippingTitle, $trackingNumber);

        //send shipment email with tracking info
        $shipmentApi->sendInfo($shipmentIncrementId);

有关所有方法,请参阅app\code\core\Mage\Sales\Model\Order\Shipment\Api.php

您可以(应该)先执行检查:

//check for existing shipments if you want
if ($order->getShipmentsCollection()->count() == 0){
}

// and/or 

if($order->canShip()){
}