用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;
}

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

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

前面:

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

检查订单的状态,如果是完整的,只需要拿到货物并发送邮件

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

我发现使用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);

方法参见appcodecoreMageSalesModelOrderShipmentApi.php

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

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

最新更新