Exemplo n.º 1
0
 /**
  * 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();
 }
Exemplo n.º 2
0
 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})");
     }
 }
Exemplo n.º 3
0
 /**
  * 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();
 }