Example #1
0
 /**
  * Update shipping address
  *
  * @return void
  */
 protected function updateShippingAddress()
 {
     $data = $this->requestData['shippingAddress'];
     $profile = $this->getCartProfile();
     $address = $profile->getShippingAddress();
     if ($address) {
         \XLite\Core\Database::getEM()->refresh($address);
     }
     if (is_array($data)) {
         $noAddress = null === $address;
         $andAsBilling = false;
         $current = new \XLite\Model\Address();
         $current->map($this->prepareAddressData($data, 'shipping'));
         $equal = null;
         foreach ($profile->getAddresses() as $addressEqual) {
             if ($addressEqual->isEqualAddress($current) && (!$address || $address->getAddressId() != $addressEqual->getAddressId())) {
                 $equal = $addressEqual;
                 break;
             }
         }
         if ($equal) {
             if ($address->getIsWork()) {
                 $profile->getAddresses()->removeElement($address);
                 \XLite\Core\Database::getEM()->remove($address);
             }
             if ($address) {
                 $andAsBilling = $address->getIsBilling();
                 $address->setIsShipping(false);
                 if ($andAsBilling) {
                     $address->setIsBilling(false);
                 }
             }
             $address = $equal;
             $address->setIsShipping(true);
             if ($andAsBilling) {
                 $address->setIsBilling($andAsBilling);
             }
             $noAddress = false;
         }
         if ($noAddress || !$address->getIsWork() && !$address->isEqualAddress($current)) {
             if (!$noAddress) {
                 $andAsBilling = $address->getIsBilling();
                 $address->setIsBilling(false);
                 $address->setIsShipping(false);
             }
             $address = new \XLite\Model\Address();
             $address->setProfile($profile);
             $address->setIsShipping(true);
             $address->setIsBilling($andAsBilling);
             $address->setIsWork(true);
             if ($noAddress || !(bool) \XLite\Core\Request::getInstance()->only_calculate) {
                 $profile->addAddresses($address);
                 \XLite\Core\Database::getEM()->persist($address);
             }
         }
         $address->map($this->prepareAddressData($data, 'shipping'));
         if ($noAddress && (\XLite\Core\Session::getInstance()->same_address || null === \XLite\Core\Session::getInstance()->same_address) && !$profile->getBillingAddress()) {
             // Same address as default behavior
             $address->setIsBilling(true);
         }
         \XLite\Core\Session::getInstance()->same_address = $this->getCart()->getProfile()->isEqualAddress();
         if ($noAddress) {
             \XLite\Core\Event::createShippingAddress(array('id' => $address->getAddressId()));
         }
     }
 }