public function actionAdd() { if (Yii::app()->user->isGuest) { echo Yii::t('wishlist', "You must be logged in to add to a wish list. Please sign in and try again."); return; } if (Yii::app()->request->isAjaxRequest) { $model = new WishlistAddForm(); // collect user input data if (isset($_POST)) { if (isset($_POST['WishlistAddForm'])) { $model->attributes = $_POST['WishlistAddForm']; } else { $model->attributes = $_POST; } if ($model->validate()) { $intProductId = $model->id; $intQty = $model->qty; $strSize = $model->size; $strColor = $model->color; $objProduct = Product::model()->findByPk($intProductId); if (!$objProduct instanceof Product) { return; } if ($objProduct->IsMaster) { if (isset($strSize) || isset($strColor)) { $objProduct = Product::LoadChildProduct($intProductId, $strSize, $strColor); if (!$objProduct instanceof Product) { echo Yii::t('wishlist', "Please choose options before selecting {button}", array('{button}' => Yii::t('product', 'Add to Wish List'))); return; } $model->id = $intProductId = $objProduct->id; } else { echo Yii::t('wishlist', "Please choose options before selecting {button}", array('{button}' => Yii::t('product', 'Add to Wish List'))); return; } } //If we don't have a wish list, create one and add. If we only have one wish list, add it. If we have more than //one, we need to prompt for which one to add $objWishLists = Wishlist::LoadUserLists(); switch (count($objWishLists)) { case 0: //We don't have a wish list, so let's create one $objWishList = new Wishlist(); $objWishList->registry_name = Yii::t('wishlist', 'My Wish List'); $objWishList->visibility = Wishlist::PERSONALLIST; $objWishList->after_purchase = Wishlist::LEAVEINLIST; $objWishList->customer_id = Yii::app()->user->id; $objWishList->gift_code = md5(uniqid()); $objWishList->ship_option = 0; if (!$objWishList->save()) { Yii::log("Error creating Wish List " . print_r($objWishList->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } $objWishLists = array(); $objWishLists[0] = $objWishList; // No break so we drop to the next set of instructions // No break so we drop to the next set of instructions case 1: $objWishList = $objWishLists[0]; $objWishItem = new WishlistItem(); $objWishItem->registry_id = $objWishList->id; $objWishItem->product_id = $intProductId; $objWishItem->qty = $intQty; if ($objWishItem->save()) { echo Yii::t('wishlist', 'Item has been added to your Wish List.'); } else { Yii::log("error saving wishlist item " . print_r($objWishItem->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); echo Yii::t('wishlist', 'An error occurred adding this item to your Wish List.'); } break; default: $Wishlistadd = new WishlistAddForm(); if (isset($_POST['WishlistAddForm'])) { $Wishlistadd->attributes = $_POST['WishlistAddForm']; } if ($Wishlistadd->validate()) { $objWishList = Wishlist::model()->findByAttributes(array('gift_code' => $Wishlistadd->gift_code)); $objWishItem = new WishlistItem(); $objWishItem->registry_id = $objWishList->id; $objWishItem->product_id = $intProductId; $objWishItem->qty = $Wishlistadd->qty; if ($objWishItem->save()) { echo Yii::t('global', 'Item has been added to your Wish List.'); } else { Yii::log("error saving wishlist item " . print_r($objWishItem->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); echo Yii::t('global', 'An error occurred adding this item to your Wish List.'); } } else { echo "multiple"; } break; } } else { echo print_r($model->getErrors(), true); } } else { echo "Error missing POST"; } } else { echo "Error not AJAX"; } }
/** * Ajax responder, when choosing a color from the dropdown, get product details to update page */ public function actionGetmatrixproduct() { if (Yii::app()->request->isAjaxRequest === false) { return; } $id = Yii::app()->getRequest()->getParam('id'); $strSize = Yii::app()->getRequest()->getParam('product_size'); $strColor = Yii::app()->getRequest()->getParam('product_color'); $objProduct = Product::LoadChildProduct($id, $strSize, $strColor); if ($objProduct instanceof Product) { $arrReturn['status'] = 'success'; $arrReturn['id'] = $objProduct->id; $arrReturn['FormattedPrice'] = $objProduct->getMarkedUpPrice(); $arrReturn['FormattedRegularPrice'] = $objProduct->SlashedPrice; $arrReturn['image_id'] = CHtml::image(Images::GetLink($objProduct->image_id, ImagesType::pdetail)); $arrReturn['code'] = $objProduct->code; $arrReturn['title'] = $objProduct->Title; $arrReturn['InventoryDisplay'] = $objProduct->InventoryDisplay; $arrReturn['FormattedSavingsAmount'] = $objProduct->getFormattedSavingsAmount(); $arrReturn['FormattedSavingsPercentage'] = $objProduct->getFormattedSavingsPercentage(); if ($objProduct->WebLongDescription) { $arrReturn['description_long'] = $objProduct->WebLongDescription; } else { $arrReturn['description_long'] = $objProduct->parent0->WebLongDescription; } if ($objProduct->description_short) { $arrReturn['description_short'] = $objProduct->WebShortDescription; } else { $arrReturn['description_short'] = $objProduct->parent0->WebShortDescription; } Yii::app()->clientscript->scriptMap['jquery.js'] = false; $arrReturn['photos'] = $this->renderPartial('/product/_photos', array('model' => $objProduct), true, false); } else { // options are missing so return the master product $objProduct = Product::model()->findByPk($id); $arrReturn['FormattedPrice'] = $objProduct->getFormattedSlashedPrice(); $arrReturn['FormattedRegularPrice'] = $objProduct->SlashedPrice; $arrReturn['FormattedSavingsAmount'] = $objProduct->getFormattedSavingsAmount(); $arrReturn['FormattedSavingsPercentage'] = $objProduct->getFormattedSavingsPercentage(); $arrReturn['code'] = $objProduct->code; $arrReturn['title'] = $objProduct->Title; $arrReturn['InventoryDisplay'] = $objProduct->InventoryDisplay; if ($objProduct->WebLongDescription) { $arrReturn['description_long'] = $objProduct->WebLongDescription; } if ($objProduct->description_short) { $arrReturn['description_short'] = $objProduct->WebShortDescription; } $arrReturn['photos'] = $this->renderPartial('/product/_photos', array('model' => $objProduct), true, false); } echo json_encode($arrReturn); }
/** * Ajax receiver function to Add To Cart. * This function adds to the cart and then returns a JSON encoded string of * the cart contents. This is typically used by the Cart Display widget. * This routine will always send back all the info, but some people may * choose to only have some details like the total and item count display. */ public function actionAddToCart() { if (Yii::app()->request->isAjaxRequest) { $intProductId = Yii::app()->getRequest()->getParam('id'); $strSize = Yii::app()->getRequest()->getParam('product_size'); $strColor = Yii::app()->getRequest()->getParam('product_color'); if (isset($strSize) || isset($strColor)) { // We passed a size and or color selection, so get the right item $objProduct = Product::LoadChildProduct($intProductId, $strSize, $strColor); if ($objProduct instanceof Product) { $intProductId = $objProduct->id; } } $intQty = Yii::app()->getRequest()->getParam('qty'); $intWishId = Yii::app()->getRequest()->getParam('wishid'); if (!isset($intWishId)) { $intWishId = null; } $intCount = Yii::app()->shoppingcart->item_count; $intRowId = Yii::app()->shoppingcart->addProduct($intProductId, $intQty, $intWishId); if ($intRowId) { if (!is_numeric($intRowId)) { //We got back an error message, not a rowid if (is_array($intRowId)) { $message = $intRowId['errorMessage']; } else { $message = $intRowId; } Yii::log("Error attempting to add product " . $intProductId . ": " . $message, 'error', 'application . ' . __CLASS__ . " . " . __FUNCTION__); $arrReturn['action'] = "alert"; $arrReturn['errormsg'] = Yii::t('global', $message); } else { Yii::log("Added item " . $intProductId . " as cart_items id " . $intRowId, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objCart = Yii::app()->shoppingcart; $objCartItem = CartItem::model()->findByPk($intRowId); //If this was a result of a Wish List add, update that record if (!is_null($intWishId)) { WishlistItem::model()->updateByPk($intWishId, array('cart_item_id' => $intRowId)); } $arrReturn['action'] = "success"; $arrReturn['totalItemCount'] = Yii::app()->shoppingcart->totalItemCount; if ($intWishId !== null) { $arrReturn['purchaseStatus'] = WishlistItem::model()->findByPk($intWishId)->PurchaseStatus; } $strCartfile = Yii::app()->getRequest()->getParam('cart'); $strCartfile = empty($strCartfile) ? "_sidecart" : $strCartfile; $arrReturn['shoppingcart'] = $this->renderPartial('/site/' . $strCartfile, array('objCartItem' => $objCartItem), true); } $this->renderJSON($arrReturn); } else { Yii::log("Error attempting to add product " . $intProductId . " for qty " . $intQty, 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); } } }