djmak
djmak

Reputation: 93

Send shipment email programmatically for the completed orders in magento

I am using magento default shipment from the admin side. so its working fine and sending email to the customers perfectly.

I want to create one script that can send email with the shipment details for all the completed orders in magento. This will be only for certain orders coming through CSV.

My script working fine when we are using the order_id of the processing orders but its not working for the completed orders . and not sending the Order items details in the email please suggest me any solution for it..

Thanks a lot.

I am using the below script to do so :

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

Upvotes: 2

Views: 8836

Answers (2)

kmdsax
kmdsax

Reputation: 1361

I found success using the order_shipment_api instead of the service_order model.

If you want to include the tracking number in the shipment email, make sure you add the tracking first and then use Mage::getModel('sales/order_shipment_api')->sendInfo($shipmentIncrementId).

Code Snippet:

        $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);

See app\code\core\Mage\Sales\Model\Order\Shipment\Api.php for all methods.

You can(should) also perform checks first:

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

// and/or 

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

Upvotes: 0

dagfr
dagfr

Reputation: 2384

Your script tries to create the shipment then send the email.

For completed orders the shipment is already created and you can't create it again, so your try fails before the email sending.

Just before :

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

Check the status of the order, if it's complete, just need to get the shipment and send the mail

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

Else you can perform your script.

Upvotes: 2

Related Questions