Exemplo n.º 1
0
		private function editOrderSaveItemCustomizationsAction()
		{
			if(empty($_POST['quoteSession'])) {
				exit;
			}

			$quote = getClass('ISC_ADMIN_ORDERS')->getQuoteSession($_POST['quoteSession']);
			if(!$quote) {
				$this->sendEditOrderNoQuoteResponse();
			}

			// Adding a new item
			$newItem = false;
			if(empty($_POST['itemId'])) {
				$newItem = true;
				$item = new ISC_QUOTE_ITEM;
				$item->setQuote($quote);

				if (empty($_POST['productId'])) {
					// Virtual item, disable checks that apply only to actual products
					$item
						->setInventoryCheckingEnabled(false)
						->setType(PT_VIRTUAL);
				} else {
					// New item is based on an existing product
					$item->setProductId($_POST['productId']);
				}
			}
			// Updating an existing item in the quote. Attempt to find it.
			else {
				$item = $quote->getItemById($_POST['itemId']);
				if(!$item || $item->isGiftCertificate()) {
					exit;
				}
			}

			$item->setQuantity($_POST['quantity']);

			// are we managing a virtual item?
			if (!$item->getProductId()) {
				// Set a custom sku for this quote item
				if (!empty($_POST['sku'])) {
					$item->setSku($_POST['sku']);
				}

				// Set a custom name for this quote item
				if (!empty($_POST['name'])) {
					$item->setName($_POST['name']);
				}
			} else {
				$query = "
					SELECT *
					FROM [|PREFIX|]products
					WHERE productid=".(int)$item->getProductId()."
				";
				$result = $this->db->query($query);
				$product = $this->db->fetch($result);
			}

			// Set the priceof the product
			$item->setBasePrice(DefaultPriceFormat($_POST['price'], null, true), true);

			// Select the variation if one was chosen
			if(isset($_POST['variationId'])) {
				$item->setVariation($_POST['variationId']);
			}

			// Set configurable fields
			$configurableFields = $this->buildProductConfigurableFieldData();
			if(!empty($configurableFields)) {
				$item->applyConfiguration($configurableFields);
			}

			// Handle event dates
			if(!empty($_POST['eventDate']) && !empty($product)) {
				$item->setEventDate($_POST['eventDate']['month'], $_POST['eventDate']['day'], $_POST['eventDate']['year']);
				$item->setEventName($product['prodeventdatefieldname']);
			}

			// And finally gift wrapping
			if(!empty($_POST['giftWrappingType'])) {
				if($_POST['giftWrappingType'] == 'none') {
					$item->removeGiftWrapping();
				}
				else {
					$item->applyGiftWrapping($_POST['giftWrappingType'], $_POST['giftWrapping'], $_POST['giftMessage']);
				}
			}

			// If this is a new item, actually add it to the quote

			if(getConfig('taxDefaultTaxDisplayCart') == TAX_PRICES_DISPLAY_INCLUSIVE) {
				$incTax = true;
			}
			else {
				$incTax = false;
			}

			if($newItem) {
				$quote->addItem($item);

				$response = array(
					'closeModal' => true,
					'itemsTable' => getClass('ISC_ADMIN_ORDERS')->generateEditOrderItemsTable($quote),
					'itemsSubtotal' => formatPrice($quote->getSubtotal($incTax)),
					'isDigital' => $quote->isDigital(),
				);

				if ($quote->getIsSplitShipping()) {
					$response['multiShippingTable'] = getClass('ISC_ADMIN_ORDERS')->renderMultiShippingTable($quote);
				}

				$this->sendEditOrderResponse($response);
			}
			else {
				// If gift wrapping we need to send back multiple items - send back the entire cart
				$response = array(
					'closeModal' => true,
					'itemsSubtotal' => formatPrice($quote->getSubtotal($incTax)),
				);

				if(isset($_POST['giftWrappingType']) && $_POST['giftWrappingType'] == 'different') {
					$response['itemsTable'] = GetClass('ISC_ADMIN_ORDERS')->generateEditOrderItemsTable($quote);
					$response['isDigital'] = $quote->isDigital();
					if ($quote->getIsSplitShipping()) {
						$response['multiShippingTable'] = getClass('ISC_ADMIN_ORDERS')->renderMultiShippingTable($quote);
					}
				}
				else {
					$response['itemsUpdateItem'] = array(
						'id' => $item->getId(),
						'content' => getClass('ISC_ADMIN_ORDERS')->generateEditOrderItemRow($item),
					);
				}

				$this->sendEditOrderResponse($response);
			}
		}