/**
  * Update profile billing address
  *
  * @return void
  */
 protected function updateBillingAddress()
 {
     $noAddress = false;
     $data = empty($this->requestData['billingAddress']) ? null : $this->requestData['billingAddress'];
     $profile = $this->getCartProfile();
     if (isset($this->requestData['same_address'])) {
         \XLite\Core\Session::getInstance()->same_address = (bool) $this->requestData['same_address'];
     }
     if ($this->requestData['same_address']) {
         // Shipping and billing are same addresses
         $address = $profile->getBillingAddress();
         if ($address) {
             // Unselect old billing address
             $address->setIsBilling(false);
         }
         $address = $profile->getShippingAddress();
         if ($address) {
             // Link shipping and billing address
             $address->setIsBilling(true);
         }
     } elseif (isset($this->requestData['same_address']) && !$this->requestData['same_address']) {
         // Unlink shipping and billing addresses
         $address = $profile->getShippingAddress();
         if ($address && $address->getIsBilling()) {
             $address->setIsBilling(false);
         }
     }
     if (is_array($data) && !$this->requestData['same_address']) {
         // Save separate billing address
         $address = $profile->getBillingAddress();
         if ($address) {
             \XLite\Core\Database::getEM()->refresh($address);
         }
         $andAsShipping = false;
         $current = new \XLite\Model\Address();
         $current->map($this->prepareAddressData($data, 'billing'));
         $equal = null;
         foreach ($profile->getAddresses() as $addressEqual) {
             if ($addressEqual->isEqualAddress($current) && (!$address || $address->getAddressId() != $addressEqual->getAddressId())) {
                 $equal = $addressEqual;
                 break;
             }
         }
         if ($equal) {
             if ($address && $address->getIsWork()) {
                 $profile->getAddresses()->removeElement($address);
                 \XLite\Core\Database::getEM()->remove($address);
             }
             if ($address) {
                 $andAsShipping = $address->getIsShipping();
                 $address->setIsBilling(false);
                 if ($andAsShipping) {
                     $address->setIsShipping(false);
                 }
             }
             $address = $equal;
             $address->setIsBilling(true);
             if ($andAsShipping) {
                 $address->setIsShipping($andAsShipping);
             }
         }
         if (!$address || !$address->getIsWork() && !$address->isEqualAddress($current)) {
             if ($address) {
                 $andAsShipping = $address->getIsShipping();
                 $address->setIsBilling(false);
                 $address->setIsShipping(false);
             }
             $address = new \XLite\Model\Address();
             $address->setProfile($profile);
             $address->setIsBilling(true);
             $address->setIsShipping($andAsShipping);
             $address->setIsWork(true);
             if (!(bool) \XLite\Core\Request::getInstance()->only_calculate) {
                 $profile->addAddresses($address);
                 \XLite\Core\Database::getEM()->persist($address);
                 $noAddress = true;
             }
         }
         $address->map($this->prepareAddressData($data, 'billing'));
         \XLite\Core\Session::getInstance()->same_address = $this->getCart()->getProfile()->isEqualAddress();
     }
     if ($noAddress) {
         \XLite\Core\Event::createBillingAddress(array('id' => $address->getAddressId()));
     }
 }
Exemple #2
0
 /**
  * Update address
  *
  * @return void
  */
 protected function updateAddress($data)
 {
     if (!is_array($data)) {
         return;
     }
     $profile = $this->getCartProfile();
     $address = $profile->getShippingAddress();
     if ($address) {
         \XLite\Core\Database::getEM()->refresh($address);
     }
     $noAddress = !isset($address);
     $andAsBilling = true;
     $current = new \XLite\Model\Address();
     $current->map($this->prepareAddressData($data));
     if ($noAddress || !$address->getIsWork() && !$address->isEqualAddress($current)) {
         $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));
     if ($noAddress && !$profile->getBillingAddress() && (is_null(\XLite\Core\Session::getInstance()->same_address) || \XLite\Core\Session::getInstance()->same_address)) {
         // Same address as default behavior
         $address->setIsBilling(true);
     }
     \XLite\Core\Session::getInstance()->same_address = $this->getCart()->getProfile()->isEqualAddress();
 }
 /**
  * Set estimate destination
  *
  * @return void
  */
 protected function doActionSetDestination()
 {
     $countryCode = \XLite\Core\Request::getInstance()->destination_country;
     if (!$countryCode && !$this->hasField('country_code')) {
         $countryCode = \XLite\Core\Config::getInstance()->Shipping->anonymous_country;
     }
     $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->find($countryCode);
     $state = null;
     if (0 < intval(\XLite\Core\Request::getInstance()->destination_state)) {
         $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find(\XLite\Core\Request::getInstance()->destination_state);
         if (isset($state) && $state->getCountry()->getCode() != $countryCode) {
             $state = null;
         }
     }
     if (!$state) {
         $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->getOtherState(strval(\XLite\Core\Request::getInstance()->destination_custom_state));
     }
     if ($country && $country->getEnabled()) {
         $address = $this->getCartProfile()->getShippingAddress();
         if (!$address) {
             $profile = $this->getCartProfile();
             $address = new \XLite\Model\Address();
             $address->setProfile($profile);
             $address->setIsShipping(true);
             $address->setIsWork(true);
             $profile->addAddresses($address);
             \XLite\Core\Database::getEM()->persist($address);
         }
         $address->setCountry($country);
         if ($state) {
             if (0 < $state->getStateId()) {
                 $address->setState($state);
             } else {
                 $address->setState(null);
                 $address->setCustomState($state->getState());
             }
         }
         $address->setZipcode(\XLite\Core\Request::getInstance()->destination_zipcode);
         $address->setType(\XLite\Core\Request::getInstance()->destination_type ?: \XLite\Core\Config::getInstance()->Shipping->anonymous_address_type);
         $address->update();
         // Update cart and do not call js-event 'updateCart' to prevent race of shipping rates calculation
         $this->updateCart(true);
         $modifier = $this->getCart()->getModifier('shipping', 'SHIPPING');
         if ($modifier) {
             $shippingAddress = \XLite\Model\Shipping::getInstance()->getDestinationAddress($modifier->getModifier());
         }
         $this->valid = true;
         $this->setInternalRedirect();
     } else {
         \XLite\Core\TopMessage::addError('Shipping address is invalid');
         $this->valid = false;
     }
 }