public function testCheckAddress() { // Prepare address and save it in database $origAddress = new \XLite\Model\Address(); $origAddress->map($this->addressFields); $origAddress->setState(\XLite\Core\Database::getRepo('XLite\\Model\\State')->findOneByCountryAndCode('US', 'NY')); $origAddress->setCountry(\XLite\Core\Database::getRepo('XLite\\Model\\Country')->find('US')); $origAddress->setProfile(\XLite\Core\Database::getRepo('XLite\\Model\\Profile')->find(1)); $address = $origAddress->cloneEntity(); $origAddress->create(); // Test: new address should not be created as it is identical $this->assertFalse($address->create(), "Check that address is not created (all fields are identical)"); foreach (\XLite\Model\Address::getAddressFields() as $field) { $address = $origAddress->cloneEntity(); if ('state_id' == $field) { $address->setState(\XLite\Core\Database::getRepo('XLite\\Model\\State')->findOneByCountryAndCode('US', 'CA')); } elseif ('country_code' == $field) { $address->setCountry(\XLite\Core\Database::getRepo('XLite\\Model\\Country')->find('GB')); } elseif ('custom_state' != $field) { $address->map($this->addressFields); $methodName = 'set' . \XLite\Core\Converter::getInstance()->convertToCamelCase($field); $this->assertTrue(method_exists($address, $methodName), "Check if method exists ({$methodName})"); $modifiedField = $this->addressFields[$field] . '2'; $address->{$methodName}($modifiedField); } // Test: new address must be created as one of fields is modified $this->assertTrue($address->create(), "Check if address is created ({$field})"); } }
/** * 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(); }
/** * Fetches an existing social login profile or creates new * * @param string $login E-mail address * @param string $socialProvider SocialLogin auth provider * @param string $socialId SocialLogin provider-unique id * @param array $profileInfo Profile info OPTIONAL * * @return \XLite\Model\Profile */ protected function getSocialLoginProfile($login, $socialProvider, $socialId, $profileInfo = array()) { $profile = \XLite\Core\Database::getRepo('XLite\\Model\\Profile')->findOneBy(array('socialLoginProvider' => $socialProvider, 'socialLoginId' => $socialId, 'order' => null)); if (!$profile) { $profile = new \XLite\Model\Profile(); $profile->setLogin($login); $profile->setSocialLoginProvider($socialProvider); $profile->setSocialLoginId($socialId); $existingProfile = \XLite\Core\Database::getRepo('XLite\\Model\\Profile')->findOneBy(array('login' => $login, 'order' => null)); if ($existingProfile) { $profile = null; } else { $profile->create(); if ($profileInfo && isset($profileInfo['given_name']) && isset($profileInfo['family_name']) && isset($profileInfo['address'])) { $address = new \XLite\Model\Address(); $address->setProfile($profile); $address->setFirstname($profileInfo['given_name']); $address->setLastname($profileInfo['family_name']); if (isset($profileInfo['address']['country']) && isset($profileInfo['address']['region'])) { $address->setCountryCode($profileInfo['address']['country']); $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->findOneByCountryAndCode($profileInfo['address']['country'], $profileInfo['address']['region']); if ($state) { $address->setState($state); } } if (isset($profileInfo['address']['locality'])) { $address->setCity($profileInfo['address']['locality']); } if (isset($profileInfo['address']['street_address'])) { $address->setStreet($profileInfo['address']['street_address']); } if (isset($profileInfo['address']['postal_code'])) { $address->setZipcode($profileInfo['address']['postal_code']); } if (isset($profileInfo['phone_number'])) { $address->setPhone($profileInfo['phone_number']); } $address->setIsShipping(true); $address->setIsBilling(true); $profile->addAddresses($address); $address->create(); } } } return $profile; }
/** * Update profiel billing address * * @return void */ protected function updateBillingAddress() { $data = $this->requestData['billingAddress']; $profile = $this->getCartProfile(); 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); } else { $this->valid = false; } } 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 (!$this->requestData['same_address'] && is_array($data)) { // Save separate billing address $address = $profile->getBillingAddress(); if ($address) { \XLite\Core\Database::getEM()->refresh($address); } $andAsShipping = false; if (!$address || $data['save_as_new']) { 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); if (!(bool) \XLite\Core\Request::getInstance()->only_calculate) { $profile->addAddresses($address); \XLite\Core\Database::getEM()->persist($address); } } $address->map($this->prepareAddressData($data)); \XLite\Core\Event::updateCart(array('billingAddress' => array('same' => $address->getIsShipping()))); } $this->updateCart(); }
/** * 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())); } }
/** * Change shipping method * * @return void */ protected function doActionChangeMethod() { if (\XLite\Core\Request::getInstance()->methodId && $this->getCart()->getShippingId() != \XLite\Core\Request::getInstance()->methodId) { $this->getCart()->setShippingId(\XLite\Core\Request::getInstance()->methodId); $address = $this->getCartProfile()->getShippingAddress(); if (!$address) { // Default address $profile = $this->getCartProfile(); $address = new \XLite\Model\Address(); $addr = $this->getAddress(); // Country $c = 'US'; if ($addr && isset($addr['country'])) { $c = $addr['country']; } elseif (\XLite\Core\Config::getInstance()->General->default_country) { $c = \XLite\Core\Config::getInstance()->General->default_country; } $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->find($c); if ($country) { $address->setCountry($country); } // State $state = null; if ($addr && !empty($addr['state'])) { $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find($addr['state']); } elseif (!$addr && \XLite\Core\Config::getInstance()->Shipping->anonymous_custom_state) { $state = new \XLite\Model\State(); $state->setState(\XLite\Core\Config::getInstance()->Shipping->anonymous_custom_state); } if ($state) { $address->setState($state); } // Zip code $address->setZipcode(\XLite\Core\Config::getInstance()->General->default_zipcode); $address->setProfile($profile); $address->setIsShipping(true); $profile->addAddresses($address); \XLite\Core\Database::getEM()->persist($address); } $this->updateCart(); \XLite\Core\Event::updateCart(array('items' => array(), 'shipping' => $this->getCart()->getShippingId())); } $this->valid = true; $this->setSilenceClose(); }
/** * Filter schema fields * * @param array $fields Schema fields to filter * * @return array */ protected function getFilteredSchemaFields($fields) { if (!isset($fields['country_code'])) { // Country code field is disabled // We need leave oonly one state field: selector or text field $deleteStateSelector = true; $address = new \XLite\Model\Address(); if ($address && $address->getCountry() && $address->getCountry()->hasStates()) { $deleteStateSelector = false; } if ($deleteStateSelector && isset($fields['state_id'])) { unset($fields['state_id']); if (isset($fields['custom_state'])) { $fields['custom_state']['additionalClass'] = 'single-state-field'; } } elseif (!$deleteStateSelector && isset($fields['custom_state'])) { unset($fields['custom_state']); if (isset($fields['state_id'])) { $fields['state_id'][\XLite\View\FormField\Select\State::PARAM_COUNTRY] = $address->getCountry()->getCode(); $fields['state_id']['additionalClass'] = 'single-state-field'; } } } return $fields; }
/** * Returns delivery source address * * @return \XLite\Model\Address */ public function getSourceAddress() { if (null === $this->sourceAddress) { $address = new \XLite\Model\Address(); $config = $this->getCompanyConfiguration(); $address->setStreet($config->location_address); $address->setCity($config->location_city); $address->setCountryCode($config->location_country); if ($config->location_state) { $address->setStateId($config->location_state); } if ($config->location_custom_state) { $address->setCustomState($config->location_custom_state); } $address->setZipcode($config->location_zipcode); $this->sourceAddress = $address; } return $this->sourceAddress; }
/** * getTestProfile * * @param int $selectedProfileId * @param int $selectedAddressesId * * @return \XLite\Model\Profile * @access protected * @see ____func_see____ * @since 1.0.0 */ protected function getTestProfile($selectedProfileId = 0, $selectedAddressesId = 0) { $profile = new \XLite\Model\Profile(); $profile->map($this->testProfileData[$selectedProfileId]); if (1 == $selectedProfileId) { $m = \XLite\Core\Database::getRepo('XLite\\Model\\Membership')->find(1); $profile->setMembership($m); $profile->setPendingMembership($m); } foreach ($this->testAddresses[$selectedAddressesId] as $data) { $address = new \XLite\Model\Address(); $address->map($data); $address->setProfile($profile); $profile->addAddresses($address); } $result = $profile->create(); $this->assertNotNull($profile, sprintf('Profile creation failed (%d, %d)', $selectedProfileId, $selectedAddressesId)); return $profile; }
/** * Change shipping method * @todo: refactor (decompose) * * @return void */ protected function doActionChangeMethod() { $methodId = \XLite\Core\Request::getInstance()->methodId; $cart = $this->getCart(); if (null !== $methodId && $cart->getShippingId() != $methodId) { $cart->setLastShippingId($methodId); $cart->setShippingId($methodId); $address = $this->getCartProfile()->getShippingAddress(); if (!$address) { // Default address $profile = $this->getCartProfile(); $address = new \XLite\Model\Address(); $addr = $this->getAddress(); // Country $c = 'US'; if ($addr && isset($addr['country'])) { $c = $addr['country']; $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->find($c); } elseif (\XLite\Model\Address::getDefaultFieldValue('country')) { $country = \XLite\Model\Address::getDefaultFieldValue('country'); } else { $country = \XLite\Core\Database::getRepo('XLite\\Model\\Country')->find($c); } if ($country) { $address->setCountry($country); } // State $state = null; if ($addr && !empty($addr['state'])) { $state = \XLite\Core\Database::getRepo('XLite\\Model\\State')->find($addr['state']); } elseif (!$addr && \XLite\Model\Address::getDefaultFieldValue('state')) { $state = \XLite\Model\Address::getDefaultFieldValue('state'); } if ($state) { $address->setState($state); } // Zip code if (\XLite\Model\Address::getDefaultFieldValue('zipcode')) { $address->setZipcode(\XLite\Model\Address::getDefaultFieldValue('zipcode')); } $address->setProfile($profile); $address->setIsShipping(true); $profile->addAddresses($address); \XLite\Core\Database::getEM()->persist($address); } $this->updateCart(); } $this->valid = true; $this->setSilenceClose(); }