Created
September 29, 2014 10:26
-
-
Save alexwilson/b42120975be73569d6da to your computer and use it in GitHub Desktop.
Magento CSV generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class csvgen { | |
public $count; | |
private $cwd; | |
private $products; | |
private $dateTime; | |
public function __construct() { | |
$this->count = 0; | |
$this->counter = 100; | |
$this->cwd = getcwd(); | |
$this->products = array(); | |
$this->dateTime = new DateTime(); | |
} | |
public function build() { | |
$this->traverse('.'); | |
} | |
private function writeln($contents = '') { | |
print_r($contents); | |
print(PHP_EOL); | |
return true; | |
} | |
private function traverse($directory = '.') { | |
// Don't attempt to recycle our DirectoryIterator as we do not wish to overwrite | |
// our current location; plus these are fairly memory un-intensive and we will | |
// unset this. | |
$directoryIterator = new DirectoryIterator($directory); | |
$pathArray = explode('/', $directoryIterator->getPathName()); | |
// Quick hack to remove Dots from our array. | |
$pathArray = array_flip($pathArray); | |
if (array_key_exists('.', $pathArray)) { | |
unset($pathArray['.']); | |
} | |
$pathArray = array_flip($pathArray); | |
// Determine category name and price. | |
$category = array_pop($pathArray); | |
$categoryArray = explode('_', $category); | |
$category = $categoryArray[0]; | |
if (!empty($pathArray)) { | |
$category = implode('/', $pathArray) . "/{$category}"; | |
} | |
$price = "0"; | |
if (!empty($categoryArray[1])) { | |
$price = $categoryArray[1]; | |
} | |
foreach($directoryIterator as $item) { | |
// If we are a directory, traverse it. | |
if ($item->isDir() and !$item->isDot()) { | |
$this->traverse($item->getPathName()); | |
} elseif ($item->isFile()) { | |
// Check if we are dealing with an image. | |
$imageType = exif_imagetype($item->getPathName()); | |
if (!empty($imageType)) { | |
$this->count++; | |
$this->addProduct($item->getPathName(), $category, $price); | |
} | |
} | |
} | |
$directoryIterator = null; | |
} | |
private function addProduct($fileName, $category, $price) { | |
$name = pathinfo($fileName, PATHINFO_FILENAME); | |
$image = str_replace('./', '/', $fileName); | |
$product = array(); | |
$product['sku'] = "SM".$this->counter; | |
$product['_attribute_set'] = "Default"; | |
$product['_type'] = "simple"; | |
$product['_category'] = $category; | |
$product['_root_category'] = "Default Category"; | |
$product['_product_websites'] = "base"; | |
$product['attribute_set'] = "Default"; | |
$product['type'] = "simple"; | |
$product['category'] = $category; | |
$product['root_category'] = "Default Category"; | |
$product['product_websites'] = "base"; | |
$product['cost'] = $price; | |
$product['created_at'] = $this->dateTime->format('Y-m-d h:i:s'); | |
$product['description'] = $name; | |
$product['has_options'] = "0"; | |
$product['image'] = $image; | |
$product['image_label'] = ""; | |
$product['is_imported'] = "Yes"; | |
$product['msrp_display_actual_price_type'] = "Use config"; | |
$product['msrp_enabled'] = "Use config"; | |
$product['name'] = $name; | |
$product['options_container'] = "Block after Info Column"; | |
$product['price'] = $price; | |
$product['required_options'] = "0"; | |
$product['short_description'] = $name; | |
$product['small_image'] = $image; | |
$product['status'] = "1"; | |
$product['tax_class_id'] = "0"; | |
$product['thumbnail'] = $image; | |
$product['updated_at'] = $this->dateTime->format('Y-m-d h:i:s'); | |
$product['url_key'] = $name; | |
$product['url_path'] = $name . ".htm"; | |
$product['visibility'] = "4"; | |
$product['weight'] = "1"; | |
$product['qty'] = "99999"; | |
$product['min_qty'] = "0"; | |
$product['use_config_min_qty'] = "1"; | |
$product['is_qty_decimal'] = "0"; | |
$product['backorders'] = "0"; | |
$product['use_config_backorders'] = "1"; | |
$product['min_sale_qty'] = "1"; | |
$product['use_config_min_sale_qty'] = "1"; | |
$product['max_sale_qty'] = "0"; | |
$product['use_config_max_sale_qty'] = "1"; | |
$product['is_in_stock'] = "1"; | |
$product['use_config_notify_stock_qty'] = "1"; | |
$product['manage_stock'] = "0"; | |
$product['use_config_manage_stock'] = "1"; | |
$product['stock_status_changed_auto'] = "0"; | |
$product['use_config_qty_increments'] = "1"; | |
$product['qty_increments'] = "0"; | |
$product['use_config_enable_qty_inc'] = "1"; | |
$product['enable_qty_increments'] = "0"; | |
$product['is_decimal_divided'] = "0"; | |
$product['_media_attribute_id'] = $this->counter; | |
$product['_media_image'] = $image; | |
$product['_media_position'] = "1"; | |
$product['_media_is_disabled'] = "0"; | |
$product['media_attribute_id'] = $this->counter; | |
$product['media_image'] = $image; | |
$product['media_position'] = "1"; | |
$product['media_is_disabled'] = "0"; | |
$product['media_gallery'] = "0"; | |
$this->products[] = $product; | |
$this->counter++; | |
} | |
public function export() { | |
ob_start(); | |
$fp = fopen('php://output', 'w'); | |
fputcsv($fp, array_keys($this->products['0'])); | |
foreach($this->products as $values){ | |
fputcsv($fp, $values); | |
} | |
fclose($fp); | |
$csv = ob_get_contents(); | |
ob_end_clean(); | |
return $csv; | |
} | |
} | |
$csvgen = new csvgen(); | |
$csvgen->build(); | |
print_r($csvgen->export()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment