asked on
<?php
//test Produ1ct
$Prod = "WIC1-DSU-T1";
$sUsername = "root";
$sPassword = "";
$sDatabase = "teksavers_test";
// Connect to database and select database
$conn = mysql_connect('localhost', $sUsername, $sPassword) or die(mysql_error());
mysql_select_db ($sDatabase) or die("Could not select database" . mysql_error());
global $conn;
// sku number
$sku = rand(1, 1000000);
//
function commasplit ($name)
{
$v1 = explode (",", $name) ;
$name = "" ;
foreach ($v1 as $t)
{
$name .= $t ;
}
return $name ;
}
//get user agent for Brokerbin
$uagent = $_SERVER['HTTP_USER_AGENT'];
// Retrieve XML
//$url = "http://2.0.ittrader.com/index.php?keycode=jakexml&keyword=$Prod";
$url = "http://2.0.ittrader.com/?key=teks382&part=$Prod&agent=$uagent";
$sxe = new SimpleXMLElement($url, NULL, TRUE);
for ($x=0;$x<count($sxe->item);$x++) {
$part_id = mysql_real_escape_string($sxe->item[$x]->part_id);
$partkey = mysql_real_escape_string($sxe->item[$x]->partkey);
$qty = mysql_real_escape_string($sxe->item[$x]->qty);
$cond = mysql_real_escape_string($sxe->item[$x]->cond);
$partsno = mysql_real_escape_string($sxe->item[$x]->partsno);
$description = mysql_real_escape_string($sxe->item[$x]->description);
$mfg = mysql_real_escape_string($sxe->item[$x]->mfg);
$country = mysql_real_escape_string($sxe->item[$x]->country);
$price = mysql_real_escape_string($sxe->item[$x]->price);
$newprice = substr($price, 1); // returns price with no $
//echo $newprice;
$newprice =commasplit($newprice);
// If Statement to only update the exact product in the system.
// Looking for a USED Product
$strSQLExists = "SELECT * FROM `xcart_products` WHERE `product` LIKE '%$Prod%' AND `cond` LIKE '%USED%'";
//$strSQLExists = "SELECT * FROM `xcart_products` ";
$rsExists = mysql_query($strSQLExists,$conn);
$data = mysql_fetch_array($rsExists);
if($data)
{
// Record found update the record using the product.php code.
echo "There is a record for <b>$Prod</b>, with Condidtion of <b>USED</b><br />So now lets update it!";
}
else
{
//record NOT found. Create a new record in Xcart
echo "Hmm.. No record Found for <b>$Prod</b>, with a Condition of <b>USED</b>. But we found one with the condition of $cond Now we need to insert a new set of records.<br /> <br />";
//START The if includes
if ($cond =="NEW") {
// Do this: check for product of NEW if exists SQL query
// if the record exists update the record.
// include("updateNEW.php");
}
else {
// Add new records for condition for NEW.
// include the NEW files to run in the statement
// include("createNEW.php");
}
if ($cond =="REF") {
// check for product of REF if exists SQL query
// Update the record.
include("updateREF.php");
}
else {
// Add new records for condition for REF.
// include the REF files to run in the statement
include("createREF.php");
}
if ($cond =="NOB") {
// Do Nothing
}
if ($cond =="CALL") {
// Do Nothing
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
} // END bROKERBIN fOR lOOP
echo "<BR /> End of the File";
?>
////////// updateNEW.php /////////////////////
<?
// Check for NEW product condition if condition exisits then update the NEW version record of the product
// we need to get the record ID:
$strSQLExists = "SELECT * FROM `xcart_products` WHERE `product` LIKE '%$Prod%' AND `cond` LIKE '%NEW%'";
//$strSQLExists = "SELECT * FROM `xcart_products` ";
$rsExists = mysql_query($strSQLExists,$conn);
$data = mysql_fetch_array($rsExists);
$productid = $data['productid'];
if($data)
{
// DO the Product.php type of update
}// end the if $data exists
if ($newprice=="CALL"){
$iSQL = "UPDATE xcart_pricing SET price = '1.00', quantity = 1 WHERE productid = $productid";
$iSQL2 = "UPDATE xcart_products SET list_price = '1.00', avail = $qty, descr = '$Prod -- $description' WHERE productid = $productid";
$iSQL3 = "UPDATE xcart_products_lng SET descr = '$Prod -- $description' WHERE productid = $productid";
$iSQL4 = "UPDATE xcart_extra_field_values SET fieldid ='1', value = '$cond' WHERE productid = $productid";
// echo "<br>$iSQL<br>";
$oRs = mysql_query($iSQL) or die(mysql_error());
$oRs2 = mysql_query($iSQL2) or die(mysql_error());
$oRs3 = mysql_query($iSQL3) or die(mysql_error());
$oRs4 = mysql_query($iSQL4) or die(mysql_error());
// echo "<br>Product is set to USED: <br>$iSQL<br>";
}
else {
$iSQL = "UPDATE xcart_pricing SET price = '$newprice', quantity = 1 WHERE productid = $productid";
$iSQL2 = "UPDATE xcart_products SET list_price = '$newprice', avail = $qty, descr = '$Prod -- $description' WHERE productid = $productid";
$iSQL3 = "UPDATE xcart_products_lng SET descr = '$Prod -- $description' WHERE productid = $productid";
$iSQL4 = "UPDATE xcart_extra_field_values SET fieldid ='1', value = '$cond' WHERE productid = $productid";
// echo "<br>$iSQL<br>";
$oRs = mysql_query($iSQL) or die(mysql_error());
$oRs2 = mysql_query($iSQL2) or die(mysql_error());
$oRs3 = mysql_query($iSQL3) or die(mysql_error());
$oRs4 = mysql_query($iSQL4) or die(mysql_error());
//echo "<br>Product is set to USED: <br>$iSQL<br>";
}
?>
//////////////////// createNEW.php //////////////////////////////////
<?
$iSQL = "INSERT INTO `teksavers_test`.`xcart_products` (`product`, `productcode`, `list_price`, `descr`, `manufacturerid`, `avail`,`cond`) VALUES ('$Prod', 'SKU$sku-$Prod', '1.00','NEW - $description','1', '$qty','$cond');";
$oRs = mysql_query($iSQL) or die(mysql_error());
// echo "SKU-$sku created <br>";
//Step 2: query table for productid based on Created SKU in this loop.
$query = "SELECT productid FROM `teksavers_test`.`xcart_products` WHERE productcode ='SKU$sku-$Prod'; ";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$prodid = $row['productid'];
}
// Step 3: Insert Price into xcart_pricing -- this is where the price is actually kept.
$iSQL2 = "INSERT INTO `teksavers_test`.`xcart_pricing` (`productid`, `quantity`, `price`) VALUES ('$prodid','1','1.00');";
$oRs2 = mysql_query($iSQL2) or die(mysql_error());
// Step 4: Insert into xcart_product_categories / The Categorie is the main one all products. Make sure that is created in Xcart.
$iSQL3 = "INSERT INTO `teksavers_test`.`xcart_products_categories` (`categoryid`,`productid`,`main`) VALUES ('1','$prodid','Y');";
$oRs3 = mysql_query($iSQL3) or die(mysql_error());
// Step 5: Query for pricing ID from xcart_pricing to use in xcart_quick_prices
$query2 = "SELECT * FROM `xcart_pricing` WHERE `productid` ='$prodid';";
$result2 = mysql_query($query2) or die(mysql_error());
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){
$PriceId = $row2['priceid'];
}
//echo "PriceId= $PriceId <br>";
// Step 6: Insert into xcart_quick_prices
$iSQL4 = "INSERT INTO `teksavers_test`.`xcart_quick_prices` (`productid`,`priceid`) VALUES ('$prodid', '$PriceId');";
$oRs4 = mysql_query($iSQL4) or die(mysql_error());
// Step 7: Insert into xcart_products_lng // this is for the Lang setting to US
$iSQL5 = "INSERT INTO `teksavers_test`.`xcart_products_lng` (`code`,`productid`,`product`,`descr`) VALUES ('US', '$prodid', '$Prod','$cond - $description');";
//echo $iSQL5;
$oRs5 = mysql_query($iSQL5) or die(mysql_error());
// Step 8: Insert into xcart_extra_field_values // this updates the condition field for the product.
$iSQL6 = "INSERT INTO `teksavers_test`.`xcart_extra_field_values` (`productid`,`fieldid`,`value`) VALUES ('$prodid', '1','$cond');";
$oRs6 = mysql_query($iSQL6) or die(mysql_error());
$mypart = $$Prod;
$myqty = $qty;
$mycond = $cond;
echo "<p class=\"smallquery\">Creating...<br> SKU: SKU-$sku <br>$mypart <br>Price: 1.00 <br>Qty: $myqty <br />Condition: $mycond <br /> $iSQL <br> $iSQL2 <br> $iSQL3 <br> $iSQL4 <br> $iSQL5 <br> $iSQL6 </p><hr>";
?>