예제 #1
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);
         }
     }
 }
 /**
  * @return int|null
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _getDataAllowedMethods()
 {
     if (empty($this->_methods) && !empty($this->_request)) {
         $quote = $this->_helperCarrierData->getQuote();
         $shippingAddress = $quote->getShippingAddress();
         if (empty($shippingAddress)) {
             return null;
         }
         $postcode = $shippingAddress->getPostcode();
         try {
             $dimensions = $this->_helperCarrierData->getDimensions($this->_helperCarrierData->getAllItems($this->_request->getAllItems()));
         } catch (\Exception $e) {
             $this->_methods = self::INVALID_METHOD;
             return null;
         }
         $clientId = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_CLIENT_ID, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
         $clientSecret = $this->_scopeConfig->getValue(\MercadoPago\Core\Helper\Data::XML_PATH_CLIENT_SECRET, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
         $mp = $this->_mpHelper->getApiInstance($clientId, $clientSecret);
         $params = ["dimensions" => $dimensions, "zip_code" => $postcode];
         $freeMethod = $this->_helperCarrierData->getFreeMethod($this->_request);
         if (!empty($freeMethod)) {
             $params['free_method'] = $freeMethod;
         }
         $response = $mp->get("/shipping_options", $params);
         if ($response['status'] == 200) {
             $this->_methods = $response['response']['options'];
         } else {
             if (isset($response['response']['message'])) {
                 $this->_registry->register('mercadoenvios_msg', $response['response']['message']);
             }
             $this->_methods = self::INVALID_METHOD;
             $this->_helperCarrierData->log('Request params: ', $params);
             $this->_helperCarrierData->log('Error response API: ', $response);
         }
     }
     return $this->_methods;
 }