0

is it possible to add custom button to order view when tracking number is added?

i have event observer(<sales_order_shipment_save_before>) that adds tracking number to order when button submit shipment has been pressed by admin like this: observer.php:

public function salesOrderShipmentSaveBefore($observer)

        {
    $trackNumber='111111111';
                        $track = Mage::getModel('sales/order_shipment_track')
                                    ->setNumber($trackNumber) 
                                    ->setCarrierCode('mycompany_mycarrier')
                                    ->setTitle('My Carrier'); 
                        $shipment->addTrack($track);

but how can i add custom button to order view after tracking number is added? i know that if use this event <core_block_abstract_to_html_before> and this function in observer.php file:

public function labelButton($observer)
    {
    $block = $observer->getEvent()->getData( 'block' );

                if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
                    && $block->getRequest()->getControllerName() == 'sales_order')
                {
                    $block->addButton('label', array(
                        'label'     => 'Label',
                        'class'     => 'go'
                    ));
                }

button is added excatly where i need, but i need this button to be visible only if the tracking number is added to order, is it doable? any help would be good. or is there some way that i could make <core_block_abstract_to_html_before> event to happen only if <sales_order_shipment_save_before> event has already happened?

EDIT

ok finally now i am moveing somwhere!

with your suggestion smiggle i edit my code like this:

public function labelButton($observer)
{
    $block = $observer->getEvent()->getData( 'block' );

    if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
        && $block->getRequest()->getControllerName() == 'sales_order')
    {
        $carrier = $block->getOrder()->getShippingCarrier();
        $order = $block->getOrder();
        $orderIncrementId=$order->getIncrementId();
        $oOrder = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
        $storeID = $oOrder->getData('store_id');
        $shipping = $oOrder->getShippingDescription();

//var_dump($shipping); die();

        if ($carrier instanceof Mage_Shippping_Model_Carrier_Abstract
            && $carrier->isTrackingAvailable()) {
                $block->addButton('label', array(
                   'label'     => 'Label',
                   'class'     => 'go'
                ));
        }
}

with var_dump function i am getting the right shiping description from "Shipping & Handling Information" field in order view, but still how can i get if there is tracking number set in that field? i did not used isTrackingAvailable function in my carrier which i made my self but my carrier extends Mage_Shipping_Model_Carrier_Abstract and also my carrier creates tracking numbers automatically like i mentioned before with <sales_order_shipment_save_before> event, so the problem is, if in that current order customer has chosed my cretaed carrier then isTrackingAvailable function returns false

0

I'm not sure if you are in the best place for do doing this, but a solution might be:

public function labelButton($observer)
{
    $block = $observer->getEvent()->getData( 'block' );

    if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
        && $block->getRequest()->getControllerName() == 'sales_order')
    {
        $carrier = $block->getOrder()->getShippingCarrier();

        if ($carrier instanceof Mage_Shippping_Model_Carrier_Abstract
            && $carrier->isTrackingAvailable()) {
                $block->addButton('label', array(
                   'label'     => 'Label',
                   'class'     => 'go'
                ));
        }

    }
}

EDIT:

You can try to get the track information with Mage_Sales_Model_Order::getTracksCollection().

  • tried your version, but error apears and says: Call to a member function getShippingCarrier() on a non-object in Observer.php file. From this i conclude that i can not get order details from <core_block_abstract_to_html_before> event – user6398 May 14 '14 at 9:45
  • i wonder maybe there is way that i could add button right after i add tracking number. inside this function: salesOrderShipmentSaveBefore(); but then there is question will button be visible in other order views where tracking number has not been added to specific order yet – user6398 May 14 '14 at 9:46
  • edited my question – user6398 May 14 '14 at 10:18
  • found solution! see answear bellow. thank you smiggle. – user6398 May 14 '14 at 13:18
0

solution for this was:

public function labelButton($observer)
{
    $block = $observer->getEvent()->getData( 'block' );

    if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
        && $block->getRequest()->getControllerName() == 'sales_order')
    {
        $carrier = $block->getOrder()->getShippingCarrier();
        $order = $block->getOrder();
        $orderIncrementId=$order->getIncrementId();
        $oOrder = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

        $shipment_collection = Mage::getResourceModel('sales/order_shipment_collection')
            ->setOrderFilter($oOrder)
            ->load();
        foreach($shipment_collection as $shipment){
            foreach($shipment->getAllTracks() as $tracking_number){
            $trackNumber = $tracking_number->getNumber();
        }
        if(isset($trackNumber)){
            //var_dump($trackNumber);
            //die();
             $block->addButton('label', array(
                   'label'     => 'Label',
                   'class'     => 'go'
                ));
    }
}       

    }

}

Your Answer

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

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