Пример #1
0
 /**
  * @param \Magento\Framework\Event\Observer $observer
  *
  * @throws \Exception
  * @throws bool
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $merchantOrder = $observer->getMerchantOrder();
     if (!count($merchantOrder['shipments']) > 0) {
         return;
     }
     $data = $observer->getPayment();
     $order = $this->_coreModel->_getOrder($data["external_reference"]);
     //if order has shipments, status is updated. If it doesn't the shipment is created.
     if ($merchantOrder['shipments'][0]['status'] == 'ready_to_ship') {
         if ($order->hasShipments()) {
             $shipment = $this->_shipment->load($order->getId(), 'order_id');
         } else {
             $shipment = $this->_shipmentFactory->create($order);
             $order->setIsInProcess(true);
         }
         $shipment->setShippingLabel($merchantOrder['shipments'][0]['id']);
         $shipmentInfo = $this->_shipmentHelper->getShipmentInfo($merchantOrder['shipments'][0]['id']);
         $this->_coreHelper->log("Shipment Info", 'mercadopago-notification.log', $shipmentInfo);
         $serviceInfo = $this->_shipmentHelper->getServiceInfo($merchantOrder['shipments'][0]['service_id'], $merchantOrder['site_id']);
         $this->_coreHelper->log("Service Info by service id", 'mercadopago-notification.log', $serviceInfo);
         if ($shipmentInfo && isset($shipmentInfo->tracking_number)) {
             $tracking['number'] = $shipmentInfo->tracking_number;
             $tracking['description'] = str_replace('#{trackingNumber}', $shipmentInfo->tracking_number, $serviceInfo->tracking_url);
             $tracking['title'] = self::CODE;
             $existingTracking = $this->_trackFactory->create()->load($shipment->getOrderId(), 'order_id');
             if ($existingTracking->getId()) {
                 $track = $shipment->getTrackById($existingTracking->getId());
                 $track->setNumber($tracking['number'])->setDescription($tracking['description'])->setTitle($tracking['title'])->save();
             } else {
                 $track = $this->_trackFactory->create()->addData($tracking);
                 $track->setCarrierCode(\MercadoPago\MercadoEnvios\Model\Carrier\MercadoEnvios::CODE);
                 $shipment->addTrack($track);
                 $shipment->save();
             }
             $this->_coreHelper->log("Track added", 'mercadopago-notification.log', $track);
         }
         $this->_transaction->addObject($order)->save();
     }
 }
Пример #2
0
 /**
  * @param \Magento\Framework\Event\Observer $observer
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $observerData = $observer->getData();
     $orderId = $observerData['orderId'];
     $shipmentData = $observerData['shipmentData'];
     $order = $this->coreModel->_getOrder($orderId);
     $method = $order->getShippingMethod();
     if ($this->shipmentHelper->isMercadoEnviosMethod($method)) {
         $methodId = $shipmentData['shipping_option']['shipping_method_id'];
         $name = $shipmentData['shipping_option']['name'];
         $order->setShippingMethod('mercadoenvios_' . $methodId);
         $estimatedDate = $this->_timezone->formatDate($shipmentData['shipping_option']['estimated_delivery']['date']);
         $estimatedDate = __('(estimated date %1)', $estimatedDate);
         $shippingDescription = 'MercadoEnvíos - ' . $name . ' ' . $estimatedDate;
         $order->setShippingDescription($shippingDescription);
         try {
             $order->save();
             $this->shipmentHelper->log('Order ' . $order->getIncrementId() . ' shipping data set ', $shipmentData);
         } catch (\Exception $e) {
             $this->shipmentHelper->log("error when update shipment data: " . $e);
             $this->shipmentHelper->log($e);
         }
     }
 }