Example #1
0
	/**
	 * Download product data from the database to SEOM
	 *
	 * Method will process the posted data and create XML to be displayed.
	 *
	 * @access public
	 * @return string XML response to display on the page for products requested
	 */

	public function DownloadProducts()
	{
		$xml = '';
		$xml = new SimpleXMLElement('<?xml version="1.0"?><SETIProducts />');

		// set our default queries
		$query = StoneEdgeProductQuery();
		$CountQuery = StoneEdgeProductQueryCount();

		if (isset($_REQUEST['startnum']) && (int)$_REQUEST['startnum'] > 0 && isset($_REQUEST['batchsize']) && (int)$_REQUEST['batchsize'] > 0) {
			$start = (int)$_REQUEST['startnum'] - 1;
			$numresults = (int)$_REQUEST['batchsize'];

			if ($start >= 0 && $numresults > 0) {
				$query = StoneEdgeProductQuery('LIMIT ' . $start . ', ' .$numresults);
			//	$CountQuery = StoneEdgeProductQueryCount('LIMIT ' . $start . ', ' .$numresults);
			}
		}

		if ($GLOBALS['ISC_CLASS_DB']->FetchOne($CountQuery) > $start) {
			//then there are products available for download, display header
			$responseNode = $xml->addChild('Response');
			$responseNode->addChild('ResponseCode', 1);
			$responseNode->addChild('ResponseDescription', 'Success');

			$products = $GLOBALS['ISC_CLASS_DB']->Query($query);

			//content
			while ($product = $GLOBALS['ISC_CLASS_DB']->Fetch($products)) {
				$fullImage = '';
				try {
					$productImage = ISC_PRODUCT_IMAGE::getBaseThumbnailImageForProduct($product['productid']);
					if ($productImage) {
						$fullImage = $productImage->getResizedUrl(ISC_PRODUCT_IMAGE_SIZE_STANDARD, true);
					}
				} catch (Exception $exception) {
					// nothing
				}

				if ($product['prodistaxable'] == 1) {
					$taxable = 'Yes';
				} else{
					$taxable = 'No';
				}

				if ($product['prodvisible'] == 0) {
					$discontinued = 'Yes';
				} else {
					$discontinued = 'No';
				}

				$productNode = $xml->addChild('Product');
				$desc = isc_html_escape($product['proddesc']); // the tags need to be escaped
				$productNode->addChild('Taxable', $taxable);
				$productNode->addChild('Discontinued', $discontinued);

				if ($product['prodvariationid'] == 0) {
					// no variations, just send the product as is
					if(isset($product['prodcode']) && $product['prodcode'] != ''){
						$productNode->addChild('Code', htmlentities($product['prodcode']));
					}else{
						$productNode->addChild('Code', htmlentities($product['prodname']));
					}
					$productNode->addChild('WebID', $product['productid']);
					$productNode->addChild('Name', $product['prodname']);
					$productNode->addChild('Price', number_format($product['prodcalculatedprice'],2));

					$productNode->addChild('Weight', $product['prodweight']);
					$productNode->addChild('Thumb', $GLOBALS['ShopPath'] . '/product_images/' . $product['imagefile']);
					$productNode->addChild('Image', $fullImage);
					$productNode->addChild('QOH', $product['prodcurrentinv']);

				} else {
					// product has variation, we need to send each variation as a different product
					$variationQuery = "SELECT vo.voptionid, vo.voname, vo.vovalue
					FROM [|PREFIX|]product_variations v
					LEFT JOIN [|PREFIX|]product_variation_options vo ON v.variationid = vo.vovariationid
					WHERE v.variationid = '" . $product['prodvariationid'] . "'
					ORDER BY vo.voname ASC";

					$varResource = $GLOBALS['ISC_CLASS_DB']->Query($variationQuery);
					$variationids = array();
					$varnames = array();
					$varvalues = array();

					while($varReturn = $GLOBALS['ISC_CLASS_DB']->Fetch($varResource)) {
						$variationids[] = $varReturn['voptionid'];
						$varnames[] = $varReturn['voname'];
						$varvalues[] = $varReturn['vovalue'];
					}

					$variationName = array();
					$variationValues = array();
					$options = explode(',',$product['vcoptionids']);
					foreach($options as $thisOption){
						$key = array_search($thisOption, $variationids);

						if($key === false || !isset($variationids[$key])) {
							continue;
						}

						$variationName[]   = $varnames[$key];
						$variationValues[] = $varvalues[$key];
					}

					$variationValue =  implode(', ', $variationValues);

					$comboPrice = 0;
					if ($product['vcpricediff'] != '' && $product['vcpricediff'] != 'fixed') {
						$comboPrice = $product['vcprice'];
						if ($product['vcpricediff'] == 'subtract') {
							$comboPrice = $product['vcprice'] * -1;
						}
					}

					$comboWeight = 0;
					if ($product['vcweightdiff'] != '' && $product['vcweightdiff'] != 'fixed') {
						$comboWeight = $product['vcweight'];
						if ($product['vcweightdiff'] == 'subtract') {
							$comboWeight = $product['vcweight'] * -1;
						}
					}

					if(isset($product['prodcode']) && $product['prodcode'] != ''){
						$productNode->addChild('Code', htmlentities($product['prodcode']));
					}else{
						$productNode->addChild('Code', htmlentities($product['prodname']) . ' [VARID:' . $product['combinationid'] . ']');
					}
					$productNode->addChild('WebID', $product['productid'] . "-" .  $product['combinationid']);
					$productNode->addChild('Name', $product['prodname'] . ' (' . $variationValue. ')');
					$productNode->addChild('Price', number_format(($product['prodcalculatedprice'] + $comboPrice),2));
					$productNode->addChild('Description', $desc);
					$productNode->addChild('Weight', ($product['prodweight']+$comboWeight));
					$productNode->addChild('Thumb', $GLOBALS['ShopPath'] . '/product_images/' . $product['imagefile']);
					$productNode->addChild('Image', $GLOBALS['ShopPath'] . '/product_images/' . $product['vcimage']);

					$productNode->addChild('QOH', $product['vcstock']);
				} // end if this product has a variation
			}

		} else {
			//no products, return the "none available" message.
			$responseNode = $xml->addChild('Response');
			$responseNode->addChild('ResponseCode', 2);
			$responseNode->addChild('ResponseDescription', 'Success');

		}
		return $xml->asXML();
	}