以编程方式发送送货/跟踪邮件

时间:2012-03-27 09:54:43

标签: magento magento-1.4

在Magento 1.4中,我成功使用此代码将订单标记为完成并向其添加送货跟踪代码:

$order = Mage::getModel('sales/order')->loadByIncrementId($increment_id);

if($order->canShip())
{
$itemQty =  $order->getItemsCollection()->count();
$ship = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$ship = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $ship->create($increment_id);
}

$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
$shipment_collection->addAttributeToFilter('order_id', $order_id);

foreach($shipment_collection as $sc) {
    $shipment = Mage::getModel('sales/order_shipment');
    $shipment->load($sc->getId());
    if($shipment->getId() != '') { 
        $track = Mage::getModel('sales/order_shipment_track')
                 ->setShipment($shipment)
                 ->setData('title', $type)
                 ->setData('number', $code)
                 ->setData('carrier_code', 'custom')
                 ->setData('order_id', $shipment->getData('order_id'))
                 ->save();
        }
} 

它工作正常,但我无法找到我需要向客户发送货件确认邮件的正确代码,如同您选中正确的方框并验证magento后端的运费。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

                if($shipment){
                    if(!$shipment->getEmailSent()){
                        $shipment->sendEmail(true);
                        $shipment->setEmailSent(true);
                        $shipment->save();                          
                    }
                }   

答案 1 :(得分:0)

我花了很多时间搞这个...而不是使用sales / order_shipment_track模型来添加跟踪,你可以使用你用来创建货物的相同Api,然后使用Api的sendInfo()使用跟踪信息发送货件电子邮件的方法。 (从OP的例子重命名为$ ship到$ shipmentApi)

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

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

完整示例:

if($order->canShip()){

    $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