2013-03-18 87 views
1

我使用管理端的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

你的腳本試圖創建出貨,然後發送電子郵件。

對於已完成的訂單,貨件已創建並且無法再次創建,因此您的try在電子郵件發送前失敗。

就在:

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

檢查訂單的狀態,如果是完整的,只是需要獲得運輸和發送郵件

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

否則,您就可以執行腳本。

+0

非常感謝dagfr ...! – djmak 2013-03-18 12:23:28

+0

@djmak,你是怎麼得到$ shipmentIncreamentId的? – 2016-11-29 13:10:55

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()){ 
} 
相關問題