2

I have created event observer to add tracking numbers automatically. I am useing this event: <sales_order_shipment_save_before>

And there is my code for adding tracking number:

public function salesOrderShipmentSaveBefore($observer)
 $shipment = $observer->getEvent()->getShipment();  

$contents = '{"error":"can not add tracking number because of..."}';
    //$contents = '{"strid":"12345678900"}';
                $response = json_decode($contents, true);

            if (isset($response['strid']))
            {
                $trackNumber=$response["strid"];
                    $track = Mage::getModel('sales/order_shipment_track')
                                ->setNumber($trackNumber) 
                                ->setCarrierCode('mycompany_mymodule')
                                ->setTitle('My Module'); 
                  $shipment->addTrack($track);
            }
            else if (isset($response['error']))
            { Mage::getSingleton('core/session')->addError($response['error']); 

//and there i want to create error message

}

if i have strid field, tracking number is added sucessfully, and this message tells me this. "The shipment has been created". enter image description here

but how to add error message to that field? With that red field that says "shipment cannot be created" and also there is my message "can not add tracking number because of..."

UPDATE Oh God, i just solved my a little part of my problem. this is what need to bee added under else:

Mage::getSingleton('core/session')->addError($response['error']); 

But now i have question how not to save shipment, what should i do that if i have error field, how to stop shipment creatin process, to get stage where automatic error "Shipment can not be created" is shown.

UPDATE2 if anyone else will have similar problem then this is solution, i just made redirect to previous page like this:

Mage::app()->getResponse()->setRedirect($_SERVER['HTTP_REFERER']);
        Mage::app()->getResponse()->sendResponse();
            exit;

error is shown and shipment isn't saved, a little bit not excatly in that way how i wanted but this works.

  • can not add tracking number because of... oh what is the complete error message :) – David Manners May 23 '14 at 14:27
  • i am sendig some data from order to server and i am getting json data back from server with track numbers or error messages, so it can be like: "missing phone number" or something, it doesn't matter that much now – user6398 May 23 '14 at 14:31
0

The Shipment is of type Mage_Sales_Model_Abstract, so you could try to set _dataSaveAllowed to false to stop the creating process.

  • $this->_getSession()->addError($this->__('Cannot save shipment.')); $this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id'))); i found this in shipment controller, as far as i understand then in case of error magento just redirects to current order view page – user6398 May 24 '14 at 19:32

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