5

In Magento 1.4, I am successfully using this code to mark an order as Complete and add a shipping tracking code to it :

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

It is working properly, but I cannot find the right piece of code I need to send shipment confirmation mail to customer, as when you check the right box and validate shipping in magento backend.

Thank you a lot in advance for your help.

Roatha Chann
  • 435
  • 6
  • 21

2 Answers2

5
                if($shipment){
                    if(!$shipment->getEmailSent()){
                        $shipment->sendEmail(true);
                        $shipment->setEmailSent(true);
                        $shipment->save();                          
                    }
                }   
magento2new
  • 517
  • 1
  • 10
  • 38
  • Thank you very much, I've put this code inside the foreach loop, it is perfectly working ! – Roatha Chann Mar 27 '12 at 12:07
  • Actually, there is a little thing : with this code I get 2 mails : they are the same but the second one has the shipment tracking code in it. Do you know how to send only this second mail ? – Roatha Chann Mar 27 '12 at 12:23
  • it seems like you have two shipment generated for the order with one shipment including tracking number and other with no tracking number.so it sends email for every shipment. – magento2new Mar 27 '12 at 12:41
  • An email is being sent when you "create shipment" then you are adding tracking information, then you "send email" again this time it has tracking information. I am not sure how but when you $shipmentId = $ship->create($increment_id); you will need to tell it not to send email. – Jpepper May 10 '12 at 18:14
0

I spent alot of time messing with this myself...instead of using the sales/order_shipment_track model to add tracking, you can use the same Api you used to create the shipment, and then follow that with the Api's sendInfo() method to send the shipment email with tracking info. (renamed $ship from OP's example to $shipmentApi)

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

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

Full example:

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

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

kmdsax
  • 1,361
  • 9
  • 12