/** * Display the logged in user's list of addresses * and handle them choosing one * * @return void */ public function actionShippingAddress() { $this->publishJS('shipping'); $this->publishJS('zippo'); $this->layout = '/layouts/checkout'; $error = null; $this->checkoutForm = MultiCheckoutForm::loadFromSessionOrNew(); $arrObjAddresses = CustomerAddress::getActiveAddresses(); // if the logged in customer has no addresses saved on file // take them to the page where they can enter an address if (count($arrObjAddresses) < 1) { $this->redirect($this->createAbsoluteUrl('/checkout/shipping')); } $arrFirst = array(); $objCart = Yii::app()->shoppingcart; // if the logged in user has a default shipping address // make it appear first foreach ($arrObjAddresses as $key => $address) { if ($address->id == $objCart->customer->default_shipping_id) { $arrFirst['first'] = $address; // assign an index to avoid accidental overwrite unset($arrObjAddresses[$key]); } } $this->checkoutForm->objAddresses = array_values($arrFirst + $arrObjAddresses); $this->checkoutForm->saveFormToSession(); // populate our form with some default values in case the user // was logged in already and bypassed checkout login if (isset($this->checkoutForm->contactEmail) === false) { $this->checkoutForm->contactEmail = $objCart->customer->email; } if (isset($this->checkoutForm->contactEmail_repeat) === false) { $this->checkoutForm->contactEmail_repeat = $objCart->customer->email; } if (isset($_POST['MultiCheckoutForm'])) { $this->checkoutForm->attributes = $_POST['MultiCheckoutForm']; } $hasErrors = false; $inStorePickupSelected = isset($_POST['storePickupCheckBox']) && $_POST['storePickupCheckBox'] == 1; // If in-store pickup was previously selected but has been deselected, make sure it's no longer used. if ($inStorePickupSelected === false && $this->checkoutForm->isStorePickupSelected()) { // TODO: Factor out this and the similar check in actionShipping. $this->checkoutForm->shippingProvider = null; $this->checkoutForm->shippingPriority = null; $this->checkoutForm->pickupFirstName = null; $this->checkoutForm->pickupLastName = null; } $addressId = Yii::app()->getRequest()->getPost('Address_id'); $wishlistId = Yii::app()->getRequest()->getPost('wishlistId'); // Store pickup. if ($inStorePickupSelected === true) { // store pickup is chosen $this->checkoutForm->fillFieldsForStorePickup(); $this->checkoutForm->setScenario('StorePickup'); $redirectUrl = $this->createUrl('/checkout/final'); if ($this->checkoutForm->validate() === false) { $hasErrors = true; } } elseif ($addressId || $wishlistId) { $customerId = null; if ($wishlistId) { $wishlist = Wishlist::model()->findByPk($wishlistId); $addressId = $wishlist->ship_option; $customerId = $wishlist->customer_id; } // An existing shipping address is chosen. $result = $this->checkoutForm->fetchCustomerShippingAddress($addressId, $customerId); if ($result === false) { $this->redirect($this->createAbsoluteUrl("/checkout/shippingaddress")); } $this->checkoutForm->setScenario('Shipping'); $redirectUrl = $this->createUrl('/checkout/shippingoptions'); if ($this->checkoutForm->validate() == false) { $hasErrors = true; } } else { $wishlist = null; $wishlistAddress = null; // If any of the cart items are on a wishlist, then we show the // wishlist address to the user. foreach ($objCart->cartItems as $item) { if ($item->wishlist_item !== null) { $wishlist = $item->wishlistItem()->registry(); $wishlistAddressId = $wishlist->ship_option; $wishlistAddress = CustomerAddress::model()->findByPk($wishlistAddressId); break; // Multiple wishlist addresses are not yet supported. } } // Nothing was posted, just render the shipping address page. $this->render('shippingaddress', array('model' => $this->checkoutForm, 'error' => $this->formatErrors(), 'wishlist' => $wishlist, 'wishlistAddress' => $wishlistAddress)); return; } // Update address ID if there are no errors. if ($hasErrors === false) { $this->checkoutForm->updateAddressId(); // An error occurred in updateAddressId. if (count($this->checkoutForm->getErrors())) { $hasErrors = true; } } // A validation error occurred. if ($hasErrors === true) { $this->render('shippingaddress', array('model' => $this->checkoutForm, 'error' => $this->formatErrors())); return; } // Update the shipping scenarios based on the new address. $this->checkoutForm->saveFormToSession(); Shipping::updateCartScenariosInSession(); // If in-store pickup was selected we need to update the cart now // before moving to checkout/final. Otherwise, the address will be // validated at the next step and the taxes updated. if ($inStorePickupSelected === true) { // Update the shopping cart taxes. Yii::app()->shoppingcart->setTaxCodeByCheckoutForm($this->checkoutForm); // Update shipping. If in-store pickup was chosen then we need to // ensure the cart shipping values are updated. // Update shipping. If in-store pickup was chosen then we need to // ensure the cart shipping values are updated. $objShipping = CartShipping::getOrCreateCartShipping(); if ($objShipping->hasErrors() === false) { $objShipping->updateShipping(); $this->checkoutForm->addErrors($objShipping->getErrors()); } else { $this->checkoutForm->addErrors($objShipping->getErrors()); } } // Save the passed scenario and redirect to the next stage. $this->checkoutForm->passedScenario = $this->checkoutForm->getScenario(); $this->redirect($redirectUrl); }