public function loadItem() { if (empty($_POST['item_id'])) { throw new CHttpException(400, 'Bad Request!.'); } $item = CartItem::model()->with('skus')->findByPk(intval($_POST['item_id'])); if (empty($item)) { throw new CHttpException(400, 'Bad Request!.'); } $item->cartProps = empty($_POST['props']) ? '' : $_POST['props']; return $item; }
/** * Calculates pending order qty to count against available * inventory by searching for Requested or Awaiting Processing orders * Because we have our cart table and our documents table, we have to get both numbers */ public function CalculateReservedInventory() { //Pending orders not yet converted to Invoice $intReservedA = $this->getDbConnection()->createCommand("SELECT SUM(qty) FROM " . CartItem::model()->tableName() . " AS a\n\t\t\t\t\tLEFT JOIN " . Cart::model()->tableName() . " AS b ON a.cart_id=b.id\n\t\t\t\t\tLEFT JOIN " . Document::model()->tableName() . " AS c ON b.document_id=c.id\n\t\t\t\t\tWHERE\n\t\t\t\t\ta.product_id=" . $this->id . " AND b.cart_type=" . CartType::order . "\n\t\t\t\t\tAND (b.status='" . OrderStatus::Requested . "' OR b.status='" . OrderStatus::AwaitingProcessing . "'\n\t\t\t\t\t\tOR b.status='" . OrderStatus::Downloaded . "')\n\t\t\t\t\tAND (c.status IS NULL OR c.status='" . OrderStatus::Requested . "');")->queryScalar(); if (empty($intReservedA)) { $intReservedA = 0; } //Unattached orders (made independently in Lightspeed) $intReservedB = $this->getDbConnection()->createCommand("SELECT SUM(qty) from " . DocumentItem::model()->tableName() . " AS a\n\t\t\t\t\tLEFT JOIN " . Document::model()->tableName() . " AS b ON a.document_id=b.id\n\t\t\t\t\tWHERE\n\t\t\t\t\ta.product_id=" . $this->id . " AND b.order_type=" . CartType::order . "\n\t\t\t\t\tAND cart_id IS NULL AND left(order_str,3)='WO-' AND (b.status='" . OrderStatus::Requested . "');")->queryScalar(); if (empty($intReservedB)) { $intReservedB = 0; } return $intReservedA + $intReservedB; }
public function actionDeleteitem() { if (Yii::app()->user->isGuest) { _xls_404(); } $model = new WishlistEditForm(); if (isset($_POST['WishlistEditForm'])) { $model->attributes = $_POST['WishlistEditForm']; if ($model->validate()) { $strCode = $model->code; $intRow = $model->id; //Make sure code we've been passed is valid $objWishlist = Wishlist::model()->findByAttributes(array('gift_code' => $strCode)); if (!$objWishlist->Visible) { _xls_404(); } $objWishrow = WishlistItem::model()->findByAttributes(array('id' => $intRow, 'registry_id' => $objWishlist->id)); if (!is_null($objWishrow->cart_item_id)) { CartItem::model()->updateByPk($objWishrow->cart_item_id, array('wishlist_item' => null)); } if (!$objWishrow->delete()) { Yii::log('Error deleting wish list item ' . print_r($objWishrow->getErrors(), true), 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__); $response_array['status'] = 'error'; $response_array['errormsg'] = print_r($objWishrow->getErrors(), true); } else { $response_array = array('status' => "success", 'code' => $objWishlist->gift_code, 'id' => $objWishrow->id, 'reload' => true); } } else { $response_array['status'] = 'error'; $response_array['errormsg'] = print_r($model->getErrors(), true); } echo json_encode($response_array); } }
/** * Remove discounts from all items in the cart. */ public function removeDiscounts() { foreach ($this->cartItems as $obj) { $objItem = CartItem::model()->findByPk($obj->id); if ($objItem->Discounted) { $objItem->discount = 0; $objItem->sell_discount = 0; $objItem->sell_total = $objItem->sell_base * $objItem->qty; $objItem->save(); } } }
/** * Product lookup and optional delete, shows inventory numbers */ public function actionProducts() { if (isset($_POST['pk']) && isset($_POST['name']) && isset($_POST['value'])) { if ($_POST['name'] == 'code' && $_POST['value'] == "") { $items = CartItem::model()->findAll("product_id=" . $_POST['pk'] . " AND (cart_type=" . CartType::order . " OR cart_type=" . CartType::awaitpayment . ")"); if ($items) { echo "You cannot delete a product that has been used on an order"; } else { _dbx("set foreign_key_checks=0;"); Product::model()->updateAll(array('image_id' => null), 'id =' . $_POST['pk']); Images::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); ProductCategoryAssn::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); ProductRelated::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); ProductRelated::model()->deleteAllByAttributes(array('related_id' => $_POST['pk'])); ProductTags::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); ProductQtyPricing::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); ProductText::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); WishlistItem::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); TaskQueue::model()->deleteAllByAttributes(array('product_id' => $_POST['pk'])); Product::model()->deleteByPk($_POST['pk']); _dbx("set foreign_key_checks=1;"); echo "delete"; } } else { echo Yii::t('admin', 'You cannot change a product code here. Delete the code to remove it manually from the Web Store database'); } } else { $model = new Product(); if (isset($_GET['q'])) { $model->code = $_GET['q']; } $this->render("products", array('model' => $model)); } }
/** * 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__); } } }