MOD: Add products to cart automatically from items listed in an uploaded text file.

This mod will allow your customers to import a text file with a structure like this:



Item #1 Name|Quantity

Item #2 Name|Quantity

Item #3 Name|Quantity



For each item, the mod will search your products for the cheapest items in stock with that name and add them to the cart.



If a person wants 6 of a product and the first cheapest product it finds with that name has only 4 in stock. It will add all 4 and then, if it found another item with the same name, it will add the next cheapest and continue doing this until all 6 are added. If the requested quantity is not available, it will add all in stock.



It has been tested with:

  • Out-of-stock items
  • Items tying for lowest price



    Great for products such as Trading Card Game cards because the name of the item that the person enters must match the name of the product in the store. This avoids adding unwanted, similarly-named products to the cart.



    To install this mod, follow these steps:


  1. Create a new directory in your store root called uploads. Set the permissions to 777.


  2. Create a new php file under the store root called upload.php.



    ```php
if(copy($_FILES["uploaded"]["tmp_name"],"./uploads/items.txt")) {
include "item_details.php";
} else {
echo "

Sorry, there was a problem uploading your file. Click here to return.

";
}
?>

```3. Create a new php file under the store root called item_details.php. Modify the settings in this file before using.



```php



Confirm Selections


//Settings
$db_host = "";
$db_name = "";
$db_user = "";
$db_password = "";
$store_path = "/store";
$return_path = "javascript:history.go(-1);";
//End Settings

function open_site($website){
$readSite = "";
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
if($openSite = @fopen ($website,"r")) {
while(!feof($openSite)) {
$readSite .= fread($openSite, 4096);
}
fclose($openSite);
}
return $readSite;
}

$tmp = open_site("uploads/items.txt");
$tmp = explode("
",$tmp);
$items = array();
for($i=0;$i if($tmp[$i] != "") {
$items[count($items)] = $tmp[$i];
}
}
for($i=1;$i for($j=0;$j<$i;$j++) {
$item1 = explode(" ",$items[$i]);
$item2 = explode(" ",$items[$j]);
$qty1 = $item1[0];
$qty2 = $item2[0];
array_shift($item1);
array_shift($item2);
$name1 = implode(" ",$item1);
$name2 = implode(" ",$item2);
$item1= trim($name1);
$item2 = trim($name2);
if($item1 == $item2) {
$items[$j] = ($qty1 + $qty2) . " $item1";
$items[$i] = "";
}
}
}
$tmp = $items;
$items = array();
for($i=0;$i if($tmp[$i] != "") {
$items[count($items)] = $tmp[$i];
}
}

define("IN_CSCART","TRUE");
require "core/db_mysql.php";
require "core/fn_database.php";
require "core/fn_common.php";

$db_conn = db_initiate($db_host, $db_user, $db_password, $db_name);
if (!$db_conn) {
fn_error(debug_backtrace(), "Cannot connect to the database server", false);
}
if (defined("MYSQL5")) {
db_query("set @@sql_mode=''");
}
?>



$s1 = 0;
$s2 = 0;
for($i=0;$i $item = explode("|",$items[$i]);
$name = trim($item[0]);
$qtyr = trim($item[1]);
$product_ids = db_get_array("SELECT product_id FROM cscart_product_descriptions WHERE product = \"$name\";");
if(count($product_ids) == 0) {
$out .= "
$name

";
$s2++;
} else {
for($j=0;$j $product_ids[$j] = $product_ids[$j]["product_id"];
}
$product_ids2 = db_get_array("SELECT product_id FROM cscart_product_prices WHERE product_id IN (" . implode(",",$product_ids) . ") ORDER BY price ASC;");
for($j=0;$j $product_ids2[$j] = $product_ids2[$j]["product_id"];
}
$qtya = 0;
for($j=0;$j $pid = $product_ids2[$j];
$qtyh = db_get_field("SELECT amount FROM cscart_products WHERE product_id = $pid AND avail = 'Y';");
$price = db_get_field("SELECT price FROM cscart_product_prices WHERE product_id = $pid;");
if($qtyh > 0 && $qtyr > 0) {
$qtyh = min($qtyh,$qtyr);
$qtyr -= $qtyh;
$qtya += $qtyh;
$s1++;
?>

}
}
if($qtya > 0) {
$out2 .= "
$name
$qtya

";
}
if($qtyr > 0) {
$out .= "
$name
$qtyr

";
$s2++;
}
}
}

if($s1 == 0) {
?>

No items will be added to the cart.


} else {
?>

The following items will be added to the cart:







}
if($s2 > 0) {
?>

The following items were either out of stock or not found:







}
?>




```4. Then insert this form code in your store wherever you want the upload form to be displayed:

[html]

Please choose a file:



[/html]