【问题标题】:Send shipment email programmatically for the completed orders in magento以编程方式为 magento 中已完成的订单发送发货电子邮件
【发布时间】:2013-03-18 06:02:52
【问题描述】:

我正在使用来自管理端的 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;
}

【问题讨论】:

    标签: magento


    【解决方案1】:

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

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

    就在之前:

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

    查看订单状态,如果已完成,只需取货并发送邮件即可

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

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

    【讨论】:

    • @djmak ,你是怎么得到 $shipmentIncreamentId 的?
    • @dagfr 这就是所谓的默认 magento 发货事件。或者如果不是那我怎么称呼它。
    【解决方案2】:

    我发现使用 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()){
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 2021-06-16
      • 2014-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      相关资源
      最近更新 更多