Example #1
0
 /**
  * Select address
  *
  * @return void
  */
 protected function doActionSelect()
 {
     $atype = \XLite\Core\Request::getInstance()->atype;
     $addressId = \XLite\Core\Request::getInstance()->addressId;
     if (\XLite\Model\Address::SHIPPING != $atype && \XLite\Model\Address::BILLING != $atype) {
         $this->valid = false;
         \XLite\Core\TopMessage::addError('Address type has wrong value');
     } elseif (!$addressId) {
         $this->valid = false;
         \XLite\Core\TopMessage::addError('Address is not selected');
     } else {
         $address = \XLite\Core\Database::getRepo('XLite\\Model\\Address')->find($addressId);
         if (!$address) {
             // Address not found
             $this->valid = false;
             \XLite\Core\TopMessage::addError('Address not found');
         } elseif (\XLite\Model\Address::SHIPPING == $atype && $this->getCart()->getProfile()->getShippingAddress() && $address->getAddressId() == $this->getCart()->getProfile()->getShippingAddress()->getAddressId()) {
             // This shipping address is already selected
             $this->silenceClose = true;
         } elseif (\XLite\Model\Address::BILLING == $atype && $this->getCart()->getProfile()->getBillingAddress() && $address->getAddressId() == $this->getCart()->getProfile()->getBillingAddress()->getAddressId()) {
             // This billing address is already selected
             $this->silenceClose = true;
         } else {
             if (\XLite\Model\Address::SHIPPING == $atype) {
                 $old = $this->getCart()->getProfile()->getShippingAddress();
                 if ($old) {
                     $old->setIsShipping(false);
                 }
                 $address->setIsShipping(true);
                 $event = array('shippingAddress' => true);
             } else {
                 $old = $this->getCart()->getProfile()->getBillingAddress();
                 if ($old) {
                     $old->setIsBilling(false);
                 }
                 $address->setIsBilling(true);
                 $event = array('billingAddress' => array('same' => $address->getIsShipping()));
             }
             \XLite\Core\Event::updateCart($event);
             \XLite\Core\Database::getEM()->flush();
             $this->updateCart();
             $this->silenceClose = true;
         }
     }
 }
Example #2
0
 /**
  * Assemble updateCart event
  *
  * @return boolean
  */
 protected function assembleEvent()
 {
     $result = false;
     $diff = array();
     $old = $this->initialCartFingerprint;
     $new = $this->getCart()->getEventFingerprint();
     $items = array();
     // Assembly changed
     foreach ($new['items'] as $n => $cell) {
         $found = false;
         foreach ($old['items'] as $i => $oldCell) {
             if ($cell['key'] == $oldCell['key']) {
                 if ($cell['quantity'] != $oldCell['quantity']) {
                     $cell['quantity_change'] = $cell['quantity'] - $oldCell['quantity'];
                     $items[] = $cell;
                 }
                 unset($old['items'][$i]);
                 $found = true;
                 break;
             }
         }
         if (!$found) {
             $cell['quantity_change'] = $cell['quantity'];
             $items[] = $cell;
         }
     }
     // Assemble removed
     foreach ($old['items'] as $cell) {
         $cell['quantity_change'] = $cell['quantity'] * -1;
         $items[] = $cell;
     }
     if ($items) {
         $diff['items'] = $items;
     }
     if ($old['total'] != $this->getCart()->getTotal()) {
         $diff['total'] = $this->getCart()->getTotal() - $old['total'];
     }
     if ($diff) {
         \XLite\Core\Event::updateCart($diff);
         $result = true;
     }
     return $result;
 }
Example #3
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();
 }
Example #4
0
 /**
  * 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();
 }
Example #5
0
 /**
  * Assemble updateCart event
  *
  * @return boolean
  */
 protected function assembleEvent()
 {
     $diff = $this->getCartFingerprintDifference($this->initialCartFingerprint, $this->getCart()->getEventFingerprint($this->getCartFingerprintExclude()));
     if ($diff) {
         $actualDiff = $this->posprocessCartFingerprintDifference($diff);
         if ($actualDiff) {
             \XLite\Core\Event::updateCart($actualDiff);
         }
     }
     return (bool) $diff;
 }