Ejemplo n.º 1
0
 protected function afterLogin($fromCookie)
 {
     if (!$fromCookie) {
         // Assign the user to the cart, if logged in
         Yii::app()->shoppingcart->assignCustomer(Yii::app()->user->id);
         // If the user is not a guest user, then update the tax destination
         // for the user's cart to display correct product prices. We can't
         // do this for guest users since it will reset their cart to the
         // store default, which is generally not what we want, especially
         // not in tax-inclusive environments when the shipping destination
         // is in a notax region.
         if ($this->getIsGuest() !== true) {
             Yii::app()->shoppingcart->setTaxCodeByDefaultShippingAddress();
         }
         // Since we have successfully logged in, see if we have a cart in progress
         $cartInProgressFound = Yii::app()->shoppingcart->loginMerge();
         // Recalculate and update the cart if prices of any cart items have changed
         if ($cartInProgressFound === true) {
             Yii::app()->shoppingcart->verifyPrices();
             Yii::log("Cart ID is " . Yii::app()->shoppingcart->id, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
         }
         // Display no-tax message if the customer's default shipping address is
         // in no-tax destination in tax-inclusive mode
         if (Yii::app()->params['TAX_INCLUSIVE_PRICING'] == 1) {
             ShoppingCart::displayNoTaxMessage();
         }
     } else {
         Yii::log("User Login using cookie", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
     }
 }
 public function actionAddress()
 {
     if (Yii::app()->user->isGuest) {
         $this->redirect($this->createUrl("/myaccount"));
     }
     $this->breadcrumbs = array(Yii::t('global', 'My Account') => $this->createUrl("/myaccount"), Yii::t('global', 'Add an address') => $this->createUrl("myaccount/address"));
     $model = new CustomerAddress();
     $model->country_id = _xls_get_conf('DEFAULT_COUNTRY', 224);
     $checkout = new CheckoutForm();
     //For logged in users we grab the current model
     $objCustomer = Customer::GetCurrent();
     $id = null;
     if (Yii::app()->getRequest()->getParam('id') != null) {
         $id = Yii::app()->getRequest()->getParam('id');
     } elseif (isset($_POST['CustomerAddress']) && isset($_POST['CustomerAddress']['id'])) {
         $id = $_POST['CustomerAddress']['id'];
     }
     $objAddress = CustomerAddress::model()->findByPk($id);
     if ($id && $objAddress instanceof CustomerAddress && $objAddress->customer_id == Yii::app()->user->id) {
         $model = $objAddress;
     }
     // collect user input data
     if (isset($_POST['CustomerAddress'])) {
         $model->setScenario('Default');
         $model->attributes = $_POST['CustomerAddress'];
         if ($model->validate()) {
             $model->customer_id = $objCustomer->id;
             if (!$model->save()) {
                 Yii::log('Error creating new customer address ' . print_r($model->getErrors(), true), 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__);
             }
             if (CPropertyValue::ensureBoolean($model->makeDefaultBilling) === true) {
                 $objCustomer->default_billing_id = $model->id;
             } elseif (CPropertyValue::ensureInteger($objCustomer->default_billing_id) === CPropertyValue::ensureInteger($model->id)) {
                 $objCustomer->default_billing_id = null;
             }
             if (CPropertyValue::ensureBoolean($model->makeDefaultShipping) === true) {
                 $objCustomer->default_shipping_id = $model->id;
             } elseif (CPropertyValue::ensureInteger($objCustomer->default_shipping_id) === CPropertyValue::ensureInteger($model->id)) {
                 $objCustomer->default_shipping_id = null;
             }
             $objCustomer->save();
             ShoppingCart::displayNoTaxMessage();
             try {
                 Yii::app()->shoppingcart->setTaxCodeByDefaultShippingAddress();
             } catch (Exception $e) {
                 Yii::log('Error updating customer cart ' . $e->getMessage(), 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__);
             }
             Yii::app()->shoppingcart->save();
             if (Yii::app()->request->isAjaxRequest === false) {
                 $this->redirect($this->createUrl("/myaccount"));
             }
         }
     }
     if ($id && $objCustomer->default_billing_id == $model->id) {
         $model->makeDefaultBilling = 1;
     }
     if ($id && $objCustomer->default_shipping_id == $model->id) {
         $model->makeDefaultShipping = 1;
     }
     // Added Ajax for Brooklyn2014
     if (Yii::app()->request->isAjaxRequest) {
         unset($model->password);
         $errors = $model->getErrors();
         if (empty($errors) === false) {
             $response = array('status' => 'error', 'errors' => $errors);
         } else {
             $response = array('status' => 'success', 'address' => $model);
         }
         $this->renderJSON($response);
     } else {
         $this->render('address', array('model' => $model, 'checkout' => $checkout));
     }
 }