Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**************
- *
- * Takes an input CSV file of shipping prices and outputs the format needed for Zen Cart's zone shipping page.
- You can copy and paste the contents of this pastebin into makeZCZones.php: https://pastebin.com/PdLBPwb0
- 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.
- usage:
- php makeZCZones.php [input.csv]
- for instance: (assuming you have a shell such as linux/OSX/'Windows Subsystem for Linux'):
- * 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.
- php7.2b makeZCZones.php thisIsMyFile.csv | less
- or:
- php makeZCZones.php thisIsMyFile.csv > outputForZCZones.txt
- *
- requires php installed on computer.
- Format of CSV:
- [ Blank Cell ] [ Zones/Countries 2 letter ISO Codes -------->
- [ Weight ] [Start of shipping table --->
- |
- |
- \/
- 2 letter ISO codes:
- https://www.nationsonline.org/oneworld/country_code_list.htm
- Example .csv file:
- https://docs.google.com/spreadsheets/d/1ZOn4aTzb_A0HQQ9sadDuvU0k0cFZ-P62To6rz4QpEXs/edit?usp=sharing
- Google Docs: File -> Download As -> Comma Seperated Values (.csv, current sheet)
- Excel: File -> Save As -> CSV
- Gnumeric: Data -> Export Data -> Export as CSV File...
- LibreOffice: File -> Save As -> Text CSV
- Example output:
- Zone Name: AU
- Shipping String:
- 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
- Zone Name: BR
- Shipping String:
- 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
- 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 :)
- https://ems.epost.go.kr/front.EmsDeliveryDelivery02.postal
- */
- class makeZCZones {
- private $zonesCount;
- private $zonesArray;
- private $weightsCount;
- private $weightsArray;
- private $shippingTable;
- private function readAndParseCSV() {
- global $argv;
- if(!isset($argv[1])) { die('No Input File!'); }
- $file = fopen($argv[1],"r");
- $countriesRow = fgetcsv($file);
- unset($countriesRow[0]); //the first element should be empty so drop it
- $this -> zonesArray = $countriesRow;
- $this -> zonesCount = count($countriesRow);
- $rowCounter = 0;
- while(!feof($file))
- {
- $row = fgetcsv($file);
- if( gettype($row) == 'array' && count($row) > 0) {
- $this -> weightsArray[$rowCounter] = $row[0];
- $this -> shippingTable[$rowCounter] = $row;
- $rowCounter++;
- }
- }
- fclose($file);
- }
- private function createZCStrings() {
- $fullOutputString = "";
- foreach($this->zonesArray as $key => $zoneName) {
- $fullOutputString .= "\r\n\r\n\r\nZone ".$key." String: ".$zoneName."\r\n\r\nShipping String:\r\n\r\n";
- $shippingString = "";
- foreach($this->shippingTable as $row) {
- $shippingString .= $row[0] . ":" . $row[$key];
- $shippingString .= ",";
- }
- $shippingString = substr($shippingString, 0, -1);
- $fullOutputString .= $shippingString."\r\n\r\n";
- }
- echo $fullOutputString;
- }
- public function __construct() {
- $this->readAndParseCSV();
- $this->createZCStrings();
- }
- }
- $makeZCZ = new makeZCZones();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement