Advertisement
buddhaflow

Untitled

Feb 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. <?php
  2.  
  3. /**************
  4. *
  5. * Takes an input CSV file of shipping prices and outputs the format needed for Zen Cart's zone shipping page.
  6.  
  7. You can copy and paste the contents of this pastebin into makeZCZones.php: https://pastebin.com/PdLBPwb0
  8.  
  9.  
  10. If you have a shipping table with many different columns of prices for various shipping weights to many different countries that you need to import into a Zen Cart Zone Shipping module, this is the script for you. However, it will make life easier for anyone using Zone Shipping, even if you only have a few zones, as it will automatically convert a properly formatted spreadsheet page into text you can easily copy and paste into the fields in Admin Panel -> Modules -> Shipping -> [select 'zones' module] -> edit.
  11.  
  12.  
  13. usage:
  14. php makeZCZones.php [input.csv]
  15.  
  16. for instance: (assuming you have a shell such as linux/OSX/'Windows Subsystem for Linux'):
  17. * If you are on Windows and don't want to mess with WSL, but have SSH/Telnet access to your web server, you can FTP the CSV to your server and then run the command via SSH.
  18.  
  19.  
  20. php7.2b makeZCZones.php thisIsMyFile.csv | less
  21.  
  22. or:
  23.  
  24. php makeZCZones.php thisIsMyFile.csv > outputForZCZones.txt
  25. *
  26. requires php installed on computer.
  27.  
  28. Format of CSV:
  29.  
  30. [ Blank Cell ] [ Zones/Countries 2 letter ISO Codes -------->
  31. [ Weight ] [Start of shipping table --->
  32. |
  33. |
  34. \/
  35.  
  36.  
  37.  
  38. 2 letter ISO codes:
  39. https://www.nationsonline.org/oneworld/country_code_list.htm
  40.  
  41. Example .csv file:
  42. https://docs.google.com/spreadsheets/d/1ZOn4aTzb_A0HQQ9sadDuvU0k0cFZ-P62To6rz4QpEXs/edit?usp=sharing
  43.  
  44. Google Docs: File -> Download As -> Comma Seperated Values (.csv, current sheet)
  45. Excel: File -> Save As -> CSV
  46. Gnumeric: Data -> Export Data -> Export as CSV File...
  47. LibreOffice: File -> Save As -> Text CSV
  48.  
  49.  
  50. Example output:
  51.  
  52.  
  53. Zone Name: AU
  54.  
  55. Shipping String:
  56.  
  57. 0.5:18500, 1:21000, 1.5:24500, 2:28500, 2.5:32500, 3:36500, 3.5:40000, 4:44000, 4.5:48000, 5:52000, 5.5:56000, 6:60000, 6.5:64000, 7:68000, 7.5:72000, 8:75500, 8.5:79500, 9:83500, 9.5:87500, 10:91500, 10.5:95500, 11:99500, 11.5:103000, 12:107000, 12.5:111000, 13:115000, 13.5:119000, 14:123000, 14.5:127000, 15:130500, 15.5:134500, 16:138500, 16.5:142500, 17:146500, 17.5:150500, 18:154500, 18.5:158500, 19:162000, 19.5:166000, 20:170000
  58.  
  59.  
  60.  
  61. Zone Name: BR
  62.  
  63. Shipping String:
  64.  
  65. 0.5:28000, 1:32000, 1.5:36000, 2:40000, 2.5:43000, 3:49500, 3.5:55500, 4:62000, 4.5:68000, 5:74500, 5.5:80500, 6:86500, 6.5:93000, 7:99000, 7.5:105000, 8:111500, 8.5:117500, 9:124000, 9.5:130000, 10:136000, 10.5:142500, 11:148500, 11.5:155000, 12:161000, 12.5:167000, 13:173500, 13.5:179500, 14:186000, 14.5:192000, 15:198500, 15.5:204500, 16:210500, 16.5:217000, 17:223000, 17.5:229500, 18:235500, 18.5:241500, 19:248000, 19.5:254000, 20:260500
  66.  
  67.  
  68. For instance, if you can find a table like this from your shipping company's website, you should be able to just copy and paste it into a spreadsheet, convert all individual column headers from the country name to the 2 digit ISO codes ZC uses (or the strings for multi-country zones: 'CN, JP, KR, TW'), export it to CSV, run this script on it, and copy and paste the output text into each zone. Done :)
  69.  
  70. https://ems.epost.go.kr/front.EmsDeliveryDelivery02.postal
  71.  
  72.  
  73. */
  74.  
  75.  
  76. class makeZCZones {
  77.  
  78. private $zonesCount;
  79. private $zonesArray;
  80.  
  81. private $weightsCount;
  82. private $weightsArray;
  83.  
  84. private $shippingTable;
  85.  
  86. private function readAndParseCSV() {
  87.  
  88. global $argv;
  89.  
  90. if(!isset($argv[1])) { die('No Input File!'); }
  91.  
  92.  
  93. $file = fopen($argv[1],"r");
  94.  
  95. $countriesRow = fgetcsv($file);
  96.  
  97. unset($countriesRow[0]); //the first element should be empty so drop it
  98.  
  99. $this -> zonesArray = $countriesRow;
  100. $this -> zonesCount = count($countriesRow);
  101.  
  102. $rowCounter = 0;
  103.  
  104. while(!feof($file))
  105. {
  106. $row = fgetcsv($file);
  107.  
  108. if( gettype($row) == 'array' && count($row) > 0) {
  109.  
  110. $this -> weightsArray[$rowCounter] = $row[0];
  111.  
  112. $this -> shippingTable[$rowCounter] = $row;
  113.  
  114. $rowCounter++;
  115. }
  116. }
  117.  
  118. fclose($file);
  119.  
  120. }
  121.  
  122. private function createZCStrings() {
  123. $fullOutputString = "";
  124. foreach($this->zonesArray as $key => $zoneName) {
  125.  
  126. $fullOutputString .= "\r\n\r\n\r\nZone ".$key." String: ".$zoneName."\r\n\r\nShipping String:\r\n\r\n";
  127. $shippingString = "";
  128.  
  129. foreach($this->shippingTable as $row) {
  130.  
  131. $shippingString .= $row[0] . ":" . $row[$key];
  132. $shippingString .= ",";
  133. }
  134.  
  135. $shippingString = substr($shippingString, 0, -1);
  136.  
  137. $fullOutputString .= $shippingString."\r\n\r\n";
  138.  
  139. }
  140.  
  141. echo $fullOutputString;
  142.  
  143. }
  144.  
  145.  
  146.  
  147. public function __construct() {
  148. $this->readAndParseCSV();
  149. $this->createZCStrings();
  150. }
  151.  
  152.  
  153.  
  154. }
  155.  
  156. $makeZCZ = new makeZCZones();
  157.  
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement