Esempio n. 1
0
 /**
  * @return \Magento\Framework\Controller\Result\Forward|void
  */
 public function execute()
 {
     $app = $this->application->getApp();
     $merchant = $app['merchant.provider']->getMerchant();
     $uuid = $this->_request->getParam('uuid');
     $event = new CustomerMigrateEvent($merchant, $uuid);
     $content = '';
     try {
         $app['dispatcher']->dispatch(CustomerMigrationSubscriber::CUSTOMER_MIGRATE_POPUP, $event);
         if (!$event->isSuccessful()) {
             throw new GenericException(Router::processError($event));
         }
         $content = $event->getContent();
     } catch (\Exception $e) {
         // log error
         var_dump($e->getMessage());
     }
     $pageId = $this->_objectManager->get('Magento\\Framework\\App\\Config\\ScopeConfigInterface')->getValue(\Magento\Cms\Helper\Page::XML_PATH_HOME_PAGE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     $page = $this->_objectManager->get(Page::class)->prepareResultPage($this, $pageId);
     if (!$page) {
         $forward = $this->resultForwardFactory->create();
         $forward->forward('defaultIndex');
         return $forward;
     }
     // XML injection doesn't work to add this javascript as we're overriding the page completely
     $js = '<script type="text/javascript">
                 (function() {
                     popupContinue = function (event) {
                         event.style.display = \'none\';
                         var loader = event.nextElementSibling;
                         loader.style.display = \'block\';
                         loader.nextElementSibling.style.display = \'none\';
                         window.location.replace(window.location.origin + window.location.pathname + \'/migrate\');
                     };
                     popupClose = function (event) {
                         window.location.replace(window.location.origin);
                     };
                     openTerms = function (event) {
                         window.open(event.href, \'_blank\');
                     };
                     openPrivacy = function (event) {
                         window.open(event.href, \'_blank\');
                     };
                     (function () {
                         // make sure our popup is on top or hierarchy
                         content = document.getElementById(\'xly\');
                         document.body.insertBefore(content, document.body.children[0]);
                     })();
                 })();
             </script>';
     $page->renderResult($this->getResponse());
     $this->getResponse()->appendBody($js);
     $this->getResponse()->appendBody($content);
     return;
 }
Esempio n. 2
0
 public function execute()
 {
     $uuid = $this->_request->getParam('uuid');
     $exists = false;
     try {
         $event = new CustomerMigrateEvent($this->merchant, $uuid);
         $this->application['dispatcher']->dispatch(CustomerMigrationSubscriber::CUSTOMER_MIGRATE_DATA, $event);
         $content = $event->getContent();
         if (!$event->isSuccessful()) {
             if (!empty($json['code']) && $json['code'] == 'USER_ALREADY_MIGRATED') {
                 $exists = true;
                 throw new UserExistsException();
             }
             throw new GenericException(Router::processError($event));
         }
         // if customer exists
         $email = $content['migration']['data']['email'];
         if (true == false) {
             $exists = true;
             $event = new CustomerMigrateEvent($this->merchant, $uuid, CustomerMigrateEvent::EXISTING_CUSTOMER);
         } else {
             $customer = $content['migration']['data']['customerData'];
             $magentoCustomer = $this->customerFactory->create();
             $magentoCustomer->setEmail($email)->setFirstname()->setLastname()->setPassword(md5('xly' . microtime()))->save();
             foreach ($customer['addresses'] as $index => $address) {
                 $magentoAddress = $this->addressFactory->create();
                 $safelyGet = function ($key) use($address) {
                     if (!empty($address[$key])) {
                         return $address[$key];
                     }
                     return '';
                 };
                 $magentoAddress->setCustomer($magentoCustomer)->setFirstname($address['firstName'])->setLastname($address['lastName'])->setCompanyName($safelyGet('company'))->setStreet([$safelyGet('address1'), $safelyGet('address2')])->setCity($safelyGet('city'))->setRegion($safelyGet('stateProvince'))->setPostcode($safelyGet('zip'))->setCountry($this->countryProvider->getIso2($address['country']))->save();
                 if ($customer['billingAddress'] == $index) {
                     $magentoCustomer->setDefaultBilling($magentoAddress->getId());
                 }
                 if ($customer['shippingAddress'] == $index) {
                     $magentoCustomer->setDefaultShipping($magentoAddress->getId());
                 }
             }
             $magentoCustomer->sendPasswordResetConfirmationEmail();
             // log user in
             $this->customerSession->loginById($magentoCustomer->getId());
         }
         // add item to cart
         if (!empty($content['cart']['productId'])) {
             $this->cart->addProduct($content['cart']['productId']);
             $this->cart->save();
         }
         // add coupon to cart
         if (!empty($content['cart']['couponCode'])) {
             $this->cart->getQuote()->setCouponCode($content['cart']['couponCode']);
             $this->cart->saveQuote();
         }
         $this->application['dispatcher']->dispatch(CustomerMigrationSubscriber::CUSTOMER_MIGRATE_SUCCESS, $event);
     } catch (GenericException $e) {
         // log error
     }
     if ($exists) {
         // show exists popup
     }
 }