0

I am using sales_order_shipment_save_after event events for update update order status after shipment.

namespace Tenx\SalesExt\Observer;
use Magento\Framework\Event\ObserverInterface;


/**
 * Webkul Marketplace AdminhtmlCustomerSaveAfterObserver Observer.
 */
class SalesOrderShipmentSaveAfter implements ObserverInterface {




    public function execute(\Magento\Framework\Event\Observer $observer) {

        $shipment = $observer->getEvent()->getShipment();
        $order = $shipment->getOrder();
        $entity = $order->getId();
        $increment_id = $order->getIncrementId();
        if(!$order->getId()){
            return;
        }
           try {

            $data = array();
            $main = array();

            $payment_code = $order->getPayment()->getMethodInstance()->getCode();
            $payment_title = $order->getPayment()->getMethodInstance()->getTitle();







            if(isset($payment_code)){

                if($payment_code == 'cashondelivery'){

                    $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()
                                ->get('Magento\Framework\App\ResourceConnection');
                        $connection = $this->_resources->getConnection();

                        $tableName = $this->_resources->getTableName('sales_order');


                        $sql1 = "Update $tableName SET state = 'pending_payment', status = 'processing_shipped' where entity_id ='" . $entity . "' and increment_id='" . $increment_id . "'";

                        $connection->query($sql1);

                }
                else{
                    echo "payemt method not cod";
                }

            }

            } 


}
}

I am getting order id and increment id but I am getting error unknown function getOrder(). Can anyone help me?

0

sales_order_shipment_save_after is dispatched when SHIPMENT is saved not the order and therefore, since this is dynamic event in shipment model, you will not get an order by simply calling getOrder on event object.

Nevertheless you can try to get an order directly from shipment if it is set

$shipment->getOrder();

If this will result in a null value then you could try

$shipment->getOrderId()

Order id must be set and so you can load appropriate order with usage of OrderRepository class.

4
  • hi .. i used above code now i am getting unknown getOrderId() .. Nov 8 '17 at 9:20
  • i am also getting this error can you tell me what is this error "Fatal error: Uncaught exception 'Exception' with message 'User Error: Some transactions have not been committed or rolled back in Nov 8 '17 at 9:25
  • It means that at some stage of processing saving of shipment some error occured and because saving is wrapped in mysql transaction therefore all of database actions are rolled back. Nevertheless, if getOrderId is unknown while calling on Shipment object then something is very wrong because this is method of its interface. With provided data i am unable to help. Here is even a similar topic: magento.stackexchange.com/questions/146034/… as you can see the solution is the same as mine so you are facing more than one problem. Nov 8 '17 at 11:16
  • i used this method_exists($shipment,'getOrder') but after shipment sometimes i am getting error can not ship this order right now Nov 8 '17 at 14:44

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.