1

I have a script to create shipments using PHP code.

I have everything working but I am not able to send Shipment emails to customers. See it says (the shipment email is not sent) ... I always have to press Send Tracking Information button to send emails, and then it starts showing (the shipment email was sent)

enter image description here

This is my complete code.

Mage::init();
$comment = null;
$email = true;
$includeComment = true;

$orderId = $data[5];  //get ordernumber from csv

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

if ($order->getId()) {

    $orderStatus = $order->getStatus();

    if ($order->getShipmentsCollection()->getData()) {
        echo 'Debug 1<br>';
        foreach ($order->getShipmentsCollection() as $shipment) {
            $shipmentid = $shipment->getId();
            echo 'Debug 2 ' . $shipmentid . '<br>';

            $shipment = Mage::getModel('sales/order_shipment')->load($shipmentid);
            $trackdata = array();
            $trackdata['carrier_code'] = $data[22];  //get CARRIER_NAME from csv
            $trackdata['title'] = $data[22];    //get title from csv
            $trackdata['number'] = $data[20];       //get CONSIGNMENT_NO from csv

            $track = Mage::getModel('sales/order_shipment_track')->addData($trackdata);
            $shipment->addTrack($track);
            $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();

            $shipment->sendEmail(true, '');
            $shipment->setEmailSent(true);
        }
    } else {

        $convertor = Mage::getModel('sales/convert_order');
        $shipment = $convertor->toShipment($order);

        foreach ($order->getAllItems() as $orderItem) {
            if (!$orderItem->getQtyToShip()) {
                continue;
            }
            if ($orderItem->getIsVirtual()) {
                continue;
            }
            $item = $convertor->itemToShipmentItem($orderItem);
            $qty = $orderItem->getQtyToShip();
            $item->setQty($qty);
            $shipment->addItem($item);
        }

        $trackdata = array();
        $trackdata['carrier_code'] = $data[22];  //get carrier_code from csv
        $trackdata['title'] = $data[22];    //get title from csv
        $trackdata['number'] = $data[20];       //get track number from csv     
        $track = Mage::getModel('sales/order_shipment_track')->addData($trackdata);
        $shipment->addTrack($track);

        $shipment->register();
        $shipment->addComment($comment, $email && $includeComment);

        $shipment->getOrder()->setIsInProcess(true);

        $transactionSave = Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();

        $shipment->sendEmail(true, '');
        $shipment->setEmailSent(true);
    }


    $order->addStatusHistoryComment("My comment why the status was changed", Mage_Sales_Model_Order::STATE_COMPLETE);
    $order->save();

    echo "<br /><br />Shipment Tracking Number Updated For the Order Id {$orderId} Successfully";

} else {
    echo 'Order with ID ' . $orderId . ' does not exists in magento <br>';
}

You can see the

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

part of code but its not working.

Where I am going wrong? I have Magento ver. 1.9.2.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.