0

I have a cronJob written in PHP which reads Tracking numbers from a CSV and updates in Magento.

My code looks like this

  if ($order->getShipmentsCollection()->getData()) {
            foreach ($order->getShipmentsCollection() as $shipment) {
                $shipmentid = $shipment->getId();
                $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($email, ($includeComment ? $comment : ''));
            }
        } 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->setEmailSent(true);
            $shipment->getOrder()->setIsInProcess(true);

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

            $shipment->sendEmail($email, ($includeComment ? $comment : ''));
        }

It works well but only thing is that does not do what "Send Tracking Information" button, this button

enter image description here

I want to replicate its behaviour.

Actually when "Send Tracking Information" button is clicked email is sent via PackPin API to customer.

But with my code it doesn't send.

1 Answer 1

0

I don't know much about PackPin or the PackPin magento module (which I'm assuming is something you're using), but a few ideas that might be worth looking at:

1) You set $shipment->setEmailSent(true); prior to calling $shipment->sendEmail($email, ($includeComment ? $comment : ''));. If the PackPin module overrides the shipment model behavior, it's possible it checks for the value of emailSent prior to sending, and only sends if $emailSent is false.

2) (Again, assuming there's a PackPin module) - It's possible the module overrides the "Send Tracking Information" button with it's own method that doesn't use $shipment->sendEmail() and makes an API call within a controller method instead. I would dig in the module more to see how it's being sent when clicking that button.

Hope this helps.

2
  • I think $shipment->setEmailSent(true); is issue ...
    – Umair Ayub
    Dec 2, 2016 at 18:58
  • Inside the IF condition in my code, that means if tracking is already added, and for the orders tracking already added, those get email successfully ... but in else condition email is not sent ...
    – Umair Ayub
    Dec 2, 2016 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.