Zen-Cart Fake Order Process


/ Published in: PHP
Save to your folder(s)

Used to generates fake orders for Zen-Cart.


Copy this code and paste it in your HTML
  1. /**
  2.  * Get one or more random date value between the start date
  3.  * and the end date
  4.  *
  5.  * The end date is not included.
  6.  *
  7.  * Date formate: yyyy-mm-dd
  8.  *
  9.  * @param date $dateStart
  10.  * @param date $dateEnd
  11.  * @param integer $number
  12.  * @return array
  13.  */
  14. function getRandomDates ($dateStart, $dateEnd, $number = 1) {
  15.  
  16. $dates = array ();
  17.  
  18. $hh1 = 8;
  19. $hh2 = 24;
  20.  
  21. $ii1 = 0;
  22. $ii2 = 60;
  23.  
  24. $ss1 = 0;
  25. $ss2 = 60;
  26.  
  27. $parts1 = explode('-',$dateStart);
  28. $parts2 = explode('-',$dateEnd);
  29.  
  30. if (sizeof($parts1) == 3 && sizeof($parts2) == 3) {
  31.  
  32. $days = abs(strtotime($dateEnd) - strtotime($dateStart)) / (3600*24);
  33. $randomDays = getRandomIntegers(0, $days, $number);
  34.  
  35. $yy1 = $parts1[0];
  36. $mm1 = $parts1[1];
  37. $dd1 = $parts1[2];
  38.  
  39. for ($i = 0; $i < $number; $i++) {
  40.  
  41. $dates[] = date('Y-m-d H:i:s', mktime(getRandomIntegers ($hh1, $hh2), getRandomIntegers ($ii1, $ii2),
  42. getRandomIntegers ($ss1, $ss2), $mm1, $dd1 + $randomDays[$i], $yy1));
  43.  
  44. }
  45.  
  46. }
  47.  
  48. return $dates;
  49. }
  50.  
  51.  
  52. /**
  53.  * Get one or more random number between the start and the end
  54.  * The end date is not included.
  55.  *
  56.  * @param integer $start
  57.  * @param integer $end
  58.  * @param integer $number
  59.  * @return array
  60.  */
  61. function getRandomIntegers ($start, $end, $number = 1) {
  62.  
  63. $ris = array();
  64.  
  65. //reduce one for mt_rand is inclusive.
  66. $end--;
  67.  
  68. for ($i = 0; $i < $number; $i++) {
  69.  
  70. $ris[] = mt_rand($start, $end);
  71.  
  72. }
  73.  
  74. if (sizeof($ris) == 1) {
  75. return $ris[0];
  76. }
  77.  
  78. return $ris;
  79.  
  80. }
  81.  
  82. /**
  83.  * Get all the subcategory IDs of the parent category whose ID
  84.  * is specified.
  85.  *
  86.  * @param integer $parentID
  87.  * @return array
  88.  */
  89. function getSubcategories ($parentID, $self = false) {
  90.  
  91.  
  92. $subs = array ();
  93.  
  94. if ($self) {
  95. $subs[] = $parentID;
  96. }
  97.  
  98. _getSubcategories ($parentID, &$subs);
  99.  
  100. return $subs;
  101.  
  102. }
  103.  
  104. /**
  105.  * Help function for getSubcategories()
  106.  *
  107.  * @param integer $pid
  108.  * @param array $cats
  109.  * @see getSubcategories()
  110.  */
  111. function _getSubcategories ($pid, &$cats) {
  112.  
  113. global $db;
  114.  
  115. $sql = "SELECT c.categories_id FROM " . TABLE_CATEGORIES . " c WHERE c.parent_id=$pid";
  116.  
  117. $result = $db->Execute($sql);
  118.  
  119. while (!$result->EOF) {
  120.  
  121. $cats[] = $result->fields['categories_id'];
  122.  
  123. _getSubcategories ($result->fields['categories_id'], &$cats);
  124.  
  125. $result->MoveNext();
  126. }
  127.  
  128. }
  129.  
  130. /**
  131.  * Get all the product ids of the specified ids
  132.  *
  133.  * @param integer $categoryID
  134.  *
  135.  */
  136. function getAllProductsIds ($categoryID) {
  137.  
  138. global $db;
  139.  
  140. $ids = array ();
  141.  
  142. $allCats = getSubcategories($categoryID, true);
  143.  
  144. $sql = "SELECT DISTINCT p2c.products_id FROM " . TABLE_PRODUCTS_TO_CATEGORIES .
  145. " p2c WHERE p2c.categories_id IN (" . implode(',', $allCats) . ")";
  146.  
  147. $result = $db->Execute ($sql);
  148.  
  149. while (!$result->EOF) {
  150.  
  151. $ids [] = $result->fields['products_id'];
  152.  
  153. $result->MoveNext();
  154.  
  155. }
  156.  
  157. return $ids;
  158.  
  159. }
  160.  
  161. /**
  162.  * Get the file extension
  163.  *
  164.  * @param string $fileName
  165.  * @return string
  166.  */
  167. function getFileExtension ($fileName) {
  168.  
  169. return strtolower(substr($fileName, strrpos($fileName, '.')));
  170.  
  171. }
  172.  
  173. /**
  174.  * Create a new file name in order to avoid file name conflict.
  175.  *
  176.  * @param string $fileName
  177.  * @return string
  178.  */
  179. function createNewFileName ($fileName) {
  180. return md5($filename . date('YmdHis'));
  181. }
  182.  
  183. /**
  184.  * main function
  185.  *
  186.  * Create Fake Orders
  187.  *
  188.  * @param integer $categoryID
  189.  * @param array $names
  190.  * @param array $countries
  191.  * @param date $fDate
  192.  * @param date $tDate
  193.  * @param integer $fQ
  194.  * @param integer $tQ
  195.  */
  196. function createFakeOrders ($categoryID, $names, $countries, $fDate, $tDate, $fQ, $tQ, $numbers = 1) {
  197.  
  198. global $db,$messageStack;
  199.  
  200. $products = getAllProductsIds ($categoryID);
  201.  
  202. $counts = sizeof($products) * $numbers;
  203.  
  204. if ($counts > 0) {
  205.  
  206. $nameIdxes = getRandomIntegers(0, sizeof($names), $counts);
  207.  
  208. $countryIdxes = getRandomIntegers(0, sizeof($countries), $counts);
  209.  
  210. $dates = getRandomDates($fDate, $tDate, $counts);
  211.  
  212. $quantities = getRandomIntegers($fQ, $tQ, $counts);
  213.  
  214. $sql = "INSERT INTO " . TABLE_ORERS_FAKE . " (products_id, name, customers_country, date, quantity) VALUES ";
  215.  
  216.  
  217. $idx = 0;
  218.  
  219. for ($i = 0; $i < sizeof($products); $i++) {
  220.  
  221. for ($k = 0; $k < $numbers; $k++) {
  222.  
  223. if ($idx == 0) {
  224. $sql .= "(". $products[$i] . ",'" . mysql_real_escape_string($names[$nameIdxes[$idx]]) . "','" .
  225. mysql_real_escape_string ($countries[$countryIdxes[$idx]]) . "','" . $dates[$idx] . "'," . $quantities[$idx] .")";
  226.  
  227. } else {
  228.  
  229. $sql .= ",(". $products[$i] . ",'" . mysql_real_escape_string ($names[$nameIdxes[$idx]]) . "','" .
  230. mysql_real_escape_string ($countries[$countryIdxes[$idx]]) . "','" . $dates[$idx] . "'," . $quantities[$idx] .")";
  231. }
  232.  
  233. $idx++;
  234. }
  235.  
  236. }
  237.  
  238. //echo $sql . '<br />';
  239. $db->Execute($sql);
  240. if ($rows > 0) {
  241. $messageStack->add_session('Fake orders generated:' . $rows, 'success');
  242. } else {
  243. $messageStack->add_session('Failed to generate fake orders', 'caution');
  244. }
  245.  
  246. zen_redirect(FILENAME_FAKE_ORDER);
  247. }
  248.  
  249. }

URL: http://ifuturetech.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.