Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once "app/Mage.php";
- Mage::app();
- $magento_file = "magento_products.csv";
- $magento_file_counter = "magento_products_%s.csv";
- $websites = array('base');
- $_attribute_set = 'Default';
- $options_container = 'Block after Info Column';
- $magento_header_arr = explode(',', 'sku,_store,_attribute_set,_type,_category,_root_category,_product_websites,color,cost,country_of_manufacture,created_at,custom_design,custom_design_from,custom_design_to,custom_layout_update,description,enable_googlecheckout,gallery,gift_message_available,has_options,image,image_label,manufacturer,media_gallery,meta_description,meta_keyword,meta_title,minimal_price,msrp,msrp_display_actual_price_type,msrp_enabled,name,news_from_date,news_to_date,options_container,page_layout,price,required_options,short_description,small_image,small_image_label,special_from_date,special_price,special_to_date,status,tax_class_id,thumbnail,thumbnail_label,turnkeye_brand,updated_at,url_key,url_path,visibility,weight,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock,notify_stock_qty,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,stock_status_changed_auto,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,_links_related_sku,_links_related_position,_links_crosssell_sku,_links_crosssell_position,_links_upsell_sku,_links_upsell_position,_associated_sku,_associated_default_qty,_associated_position,_tier_price_website,_tier_price_customer_group,_tier_price_qty,_tier_price_price,_group_price_website,_group_price_customer_group,_group_price_price,_media_attribute_id,_media_image,_media_lable,_media_position,_media_is_disabled,_custom_option_store,_custom_option_type,_custom_option_title,_custom_option_is_required,_custom_option_price,_custom_option_sku,_custom_option_max_characters,_custom_option_sort_order,_custom_option_row_title,_custom_option_row_price,_custom_option_row_sku,_custom_option_row_sort,_super_products_sku,_super_attribute_code,_super_attribute_option,_super_attribute_price_corr');
- $magento_header = array();
- foreach ($magento_header_arr as $k => $v) {
- $magento_header[$v] = $k;
- }
- $convert_header = array(
- '!PRODUCT' => 1,
- '!DESCR' => 1,
- '!FULLDESCR' => 1,
- '!TITLE_TAG' => 1,
- '!META_KEYWORDS' => 1,
- '!META_DESCRIPTION' => 1,
- );
- $association = array(
- '!PRODUCTCODE' => 'sku',
- '!PRODUCT' => 'name',
- '!CLEAN_URL' => 'url_key',
- '!WEIGHT' => 'weight',
- '!LIST_PRICE' => 'price',
- '!DESCR' => 'short_description',
- '!FULLDESCR' => 'description',
- '!TITLE_TAG' => 'meta_title',
- '!META_KEYWORDS' => 'meta_keyword',
- '!META_DESCRIPTION' => 'meta_description',
- '!AVAIL' => 'qty',
- '!LOW_AVAIL_LIMIT' => 'min_qty',
- '!MIN_AMOUNT' => 'min_sale_qty',
- '!PRICE' => 'special_price',
- '!MANUFACTURER' => 'turnkeye_brand',
- );
- $empty_magento_line = array();
- foreach ($magento_header_arr as $k => $v) {
- $empty_magento_line[$k] = '';
- }
- $product_magento_line = $empty_magento_line;
- $product_magento_line[$magento_header['_attribute_set']] = $_attribute_set;
- $product_magento_line[$magento_header['enable_googlecheckout']] = 1;
- $product_magento_line[$magento_header['_type']] = "simple";
- $product_magento_line[$magento_header['has_options']] = 0;
- $product_magento_line[$magento_header['required_options']] = 0;
- $product_magento_line[$magento_header['tax_class_id']] = 2;
- $product_magento_line[$magento_header['visibility']] = 4;
- $product_magento_line[$magento_header['use_config_min_qty']] = 1;
- $product_magento_line[$magento_header['is_qty_decimal']] = 0;
- $product_magento_line[$magento_header['backorders']] = 0;
- $product_magento_line[$magento_header['use_config_backorders']] = 1;
- $product_magento_line[$magento_header['min_sale_qty']] = '0.0000';
- $product_magento_line[$magento_header['use_config_min_sale_qty']] = 1;
- $product_magento_line[$magento_header['max_sale_qty']] = '0.0000';
- $product_magento_line[$magento_header['use_config_max_sale_qty']] = 1;
- $product_magento_line[$magento_header['is_in_stock']] = 1;
- $product_magento_line[$magento_header['use_config_notify_stock_qty']] = 1;
- $product_magento_line[$magento_header['manage_stock']] = 0;
- $product_magento_line[$magento_header['use_config_manage_stock']] = 1;
- $product_magento_line[$magento_header['stock_status_changed_auto']] = 0;
- $product_magento_line[$magento_header['use_config_qty_increments']] = 1;
- $product_magento_line[$magento_header['qty_increments']] = '0.0000';
- $product_magento_line[$magento_header['use_config_enable_qty_inc']] = 1;
- $product_magento_line[$magento_header['enable_qty_increments']] = 0;
- $product_magento_line[$magento_header['is_decimal_divided']] = 1;
- $product_magento_line['websites'] = $websites;
- $product_magento_line['options'] = array();
- $product_magento_line['media'] = array();
- $product_magento_line['conf_attributes'] = array();
- $product_magento_line['conf_skus'] = array();
- $products_file = "xcart_products.csv";
- $option_file = "xcart_product_options.csv";
- $variant_file = "xcart_product_variants.csv";
- $path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
- $fd = fopen($path . $products_file, "rb");
- $delimiter = ',';
- $header_arr = fgetcsv($fd, 10000, $delimiter);
- $header = array();
- foreach ($header_arr as $k => $v) {
- $header[$v] = $k;
- }
- $products = array();
- $prev_id = null;
- $used_sku = array();
- while($row = fgetcsv($fd, 40960, $delimiter)) {
- if ($row[$header['!PRODUCTID']]) {
- $prev_id = $row[$header['!PRODUCTID']];
- $new_row = $product_magento_line;
- foreach ($association as $xcart_field => $magento_field) {
- if (isset($convert_header[$xcart_field])) {
- $new_row[$magento_header[$magento_field]] = mb_convert_encoding($row[$header[$xcart_field]], "UTF8");
- } else {
- $new_row[$magento_header[$magento_field]] = $row[$header[$xcart_field]];
- }
- }
- if (isset($used_sku[strtolower($row[$header['!PRODUCTCODE']])])) {
- $new_row[$magento_header['sku']] = $new_row[$magento_header['sku']]. "-" . $used_sku[strtolower($row[$header['!PRODUCTCODE']])];
- $used_sku[strtolower($row[$header['!PRODUCTCODE']])]++;
- } else {
- $used_sku[strtolower($row[$header['!PRODUCTCODE']])] = 1;
- }
- $new_row['categories'] = array();
- $new_row['categories'][] = $row[$header['!CATEGORY']];
- $new_row[$magento_header['meta_description']] = Mage::helper('core/string')->substr(Mage::helper('core/string')->cleanString($new_row[$magento_header['meta_description']]), 0, 255);
- if (!$new_row[$magento_header['description']]) {
- if (!$new_row[$magento_header['short_description']]) {
- $new_row[$magento_header['short_description']] = "No information";
- }
- $new_row[$magento_header['description']] = $new_row[$magento_header['short_description']];
- }
- if ($row[$header['!FORSALE']] == 'Y') {
- $new_row[$magento_header['status']] = 1;
- } else {
- $new_row[$magento_header['status']] = 2;
- }
- $new_row[$magento_header['created_at']] = date("Y-m-d H:i:s", strtotime($row[$header['!ADD_DATE']]));
- $products[$prev_id] = $new_row;
- } else {
- $products[$prev_id]['categories'][] = $row[$header['!CATEGORY']];
- }
- }
- fclose($fd);
- $fd = fopen($path . $option_file, "rb");
- $header_arr = fgetcsv($fd, 10000, $delimiter);
- $header = array();
- foreach ($header_arr as $k => $v) {
- $header[$v] = $k;
- }
- $prev_id = null;
- $prev_class = null;
- $is_option = false;
- $prods2toclasses = array();
- while($row = fgetcsv($fd, 40960, $delimiter)) {
- if ($row[$header['!PRODUCTID']]) {
- $prev_id = $row[$header['!PRODUCTID']];
- }
- if ($row[$header['!CLASS']]) {
- $prev_class = $row[$header['!CLASS']];
- if ($row[$header['!TYPE']] == 'Y') {
- $is_option = true;
- } else {
- $is_option = false;
- }
- }
- if ($is_option && isset($products[$prev_id])) {
- if ($row[$header['!CLASS']]) {
- $products[$prev_id]['options'][$prev_class] = array(
- 'name' => mb_convert_encoding($row[$header['!DESCR']], "UTF8"),
- 'position' => $row[$header['!ORDERBY']],
- 'values' => array(),
- );
- }
- $products[$prev_id]['options'][$prev_class]['values'][] = mb_convert_encoding($row[$header['!OPTION']], "UTF8");
- }
- if (!isset($prods2toclasses[$prev_id])) {
- $prods2toclasses[$prev_id] = array();
- }
- if ($row[$header['!CLASS']]) {
- $prods2toclasses[$prev_id][$row[$header['!CLASS']]] = mb_convert_encoding($row[$header['!DESCR']], "UTF8");
- }
- }
- fclose($fd);
- $fd = fopen($path . $variant_file, "rb");
- $header_arr = fgetcsv($fd, 10000, $delimiter);
- $header = array();
- foreach ($header_arr as $k => $v) {
- $header[$v] = $k;
- }
- $code2id = array();
- $installer = Mage::getResourceModel('catalog/setup', 'core_setup');
- /**
- * @var $connection Varien_Db_Adapter_Interface
- */
- $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
- $optionTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option');
- $optionValueTable = Mage::getSingleton('core/resource')->getTableName('eav/attribute_option_value');
- function getAttributeCodeValue($code, $value)
- {
- global $code2id, $installer, $optionTable, $optionValueTable, $connection;
- $value = mb_convert_encoding($value, "UTF8");
- if (!isset($code2id[$code])) {
- $code2id[$code] = $installer->getAttribute('catalog_product', $code, 'attribute_id');
- }
- $res = $connection->query("select a.option_id from " . $optionTable . " AS a INNER JOIN " . $optionValueTable . " AS b ON a.option_id = b.option_id WHERE a.attribute_id = " . $code2id[$code] . " AND b.value = " . $connection->quote($value) . "");
- $opt_val = $res->fetch(PDO::FETCH_ASSOC);
- if ($opt_val && $opt_val['option_id']) {
- $option_id = $opt_val['option_id'];
- } else {
- $data = array(
- 'attribute_id' => $code2id[$code],
- 'sort_order' => 0,
- );
- $connection->insert($optionTable, $data);
- $magento_id = $connection->lastInsertId();
- $data = array(
- 'option_id' => $magento_id,
- 'store_id' => 0,
- 'value' => $value,
- );
- $connection->insert($optionValueTable, $data);
- $option_id = $magento_id;
- }
- $res = $connection->query("select b.value from " . $optionTable . " AS a INNER JOIN " . $optionValueTable . " AS b ON a.option_id = b.option_id WHERE a.attribute_id = " . $code2id[$code] . " AND b.option_id = " . $connection->quote($option_id) . "");
- $opt_val = $res->fetch(PDO::FETCH_ASSOC);
- if (!isset($opt_val['value'])) {
- echo 'Cannot find value: ' . $value . "\n";
- }
- return $opt_val['value'];
- }
- /**
- * @var $installer Mage_Catalog_Model_Resource_Setup
- */
- $used_classes = array();
- $used_classes_options = array();
- while($row = fgetcsv($fd, 40960, $delimiter)) {
- //print_r($row);
- $prod_id = $row[$header['!PRODUCTID']];
- if (!isset($products[$prod_id])) {
- continue;
- }
- $attribute_code = substr(preg_replace("/_+/", "_", preg_replace('/[^a-z0-9]/', '_', trim(strtolower("conf_" . $row[$header['!CLASS']])))), 0, 30);
- $attribute_name = $row[$header['!CLASS']];
- if (isset($prods2toclasses[$prod_id]) && isset($prods2toclasses[$prod_id][$attribute_name])) {
- $attribute_name = $prods2toclasses[$prod_id][$attribute_name];
- }
- if (!isset($used_classes[$attribute_code])) {
- $used_classes[$attribute_code] = $attribute_name;
- if ($installer->getAttribute('catalog_product', $attribute_code)) {
- $code2id[$attribute_code] = $installer->getAttribute('catalog_product', $attribute_code, 'attribute_id');
- continue;
- }
- $installer->addAttribute('catalog_product', $attribute_code, array(
- 'label' => $attribute_name,
- 'required' => false,
- 'input' => 'select',
- 'default' => '',
- 'position' => 1,
- 'sort_order' => 3,
- 'group' => 'General',
- 'user_defined' => true
- ));
- $code2id[$attribute_code] = $installer->getAttribute('catalog_product', $attribute_code, 'attribute_id');
- }
- if (!isset($used_classes_options[$attribute_code])) {
- $used_classes_options[$attribute_code] = array();
- }
- $used_classes_options[$attribute_code][] = mb_convert_encoding($row[$header['!OPTION']], "UTF8");
- if (!isset($magento_header[$attribute_code])) {
- $magento_header[$attribute_code] = count($magento_header);
- $magento_header_arr[$magento_header[$attribute_code]] = $attribute_code;
- }
- $products[$prod_id][$magento_header['_type']] = "configurable";
- $products[$prod_id]['conf_attributes'][$attribute_code] = $attribute_code;
- $simple = $products[$prod_id];
- $simple['conf_attributes'] = array();
- $simple['conf_skus'] = array();
- $simple['conf_attributes_options'] = array();
- $simple['categories'] = array();
- $simple[$magento_header['_type']] = "simple";
- $simple[$magento_header['visibility']] = "1";
- $simple[$magento_header['price']] = $row[$header['!PRICE']];
- $simple[$magento_header['special_price']] = '';
- $simple[$magento_header['qty']] = $row[$header['!AVAIL']];
- $simple[$magento_header['sku']] = $simple[$magento_header['sku']] . "-" . $row[$header['!VARIANTID']];
- foreach ($magento_header_arr as $mhk => $mhv) {
- if (!isset($simple[$mhk])) {
- $simple[$mhk] = '';
- }
- }
- $simple[$magento_header[$attribute_code]] = getAttributeCodeValue($attribute_code, $row[$header['!OPTION']]);
- $products[$prod_id]['conf_skus'][] = $simple[$magento_header['sku']];
- $products[$prod_id . "-" . $row[$header['!VARIANTID']]] = $simple;
- if(!empty($simple[$magento_header[$attribute_code]])) {
- $products[$prod_id]['conf_attributes_options'][] = $simple[$magento_header[$attribute_code]];
- }
- //print_r($products[$prod_id]);
- }
- fclose($fd);
- /*foreach ($used_classes as $code => $name) {
- if ($installer->getAttribute('catalog_product', $code)) {
- $code2id[$code] = $installer->getAttribute('catalog_product', $code, 'attribute_id');
- continue;
- }
- $installer->addAttribute('catalog_product', $code, array(
- 'label' => $name,
- 'required' => false,
- 'input' => 'select',
- 'default' => '',
- 'position' => 1,
- 'sort_order' => 3,
- 'group' => 'General',
- 'user_defined' => true
- ));
- $code2id[$code] = $installer->getAttribute('catalog_product', $code, 'attribute_id');
- }*/
- /*
- foreach ($used_classes_options as $code => $values) {
- foreach ($values as $value) {
- $res = $connection->query("select COUNT(*) as cnt from " . $optionTable . " AS a INNER JOIN " . $optionValueTable . " AS b ON a.option_id = b.option_id WHERE a.attribute_id = " . $code2id[$code] . " AND b.value = " . $connection->quote($value) . "");
- $opt_val = $res->fetch(PDO::FETCH_ASSOC);
- if ($opt_val['cnt']) {
- continue;
- }
- $data = array(
- 'attribute_id' => $code2id[$code],
- 'sort_order' => 0,
- );
- $connection->insert($optionTable, $data);
- $magento_id = $connection->lastInsertId();
- $data = array(
- 'option_id' => $magento_id,
- 'store_id' => 0,
- 'value' => $value,
- );
- $connection->insert($optionValueTable, $data);
- }
- }
- */
- function getImageImportName($image)
- {
- $image = str_replace("./images/", '/', $image);
- return str_replace("./files/images/", '/', $image);
- }
- $a = $connection->query("select image_path, id, alt, orderby, avail from xcart_images_t");
- $_media_attribute_id = 88;
- while ($roes1 = $a->fetch(PDO::FETCH_ASSOC)) {
- if (!isset($products[$roes1['id']])) {
- continue;
- }
- $products[$roes1['id']][$magento_header['thumbnail']] = getImageImportName($roes1['image_path']);
- $products[$roes1['id']][$magento_header['thumbnail_label']] = $roes1['alt'];
- $products[$roes1['id']]['media'][] = array(
- '_media_attribute_id' => $_media_attribute_id,
- '_media_image' => getImageImportName($roes1['image_path']),
- '_media_lable' => $roes1['alt'],
- '_media_position' => $roes1['orderby'],
- '_media_is_disabled' => ($roes1['avail'] == 'Y')?0:1,
- );
- }
- $a = $connection->query("select image_path, id, alt, orderby, avail from xcart_images_p");
- $_media_attribute_id = 88;
- while ($roes1 = $a->fetch(PDO::FETCH_ASSOC)) {
- if (!isset($products[$roes1['id']])) {
- continue;
- }
- $products[$roes1['id']][$magento_header['small_image']] = getImageImportName($roes1['image_path']);
- $products[$roes1['id']][$magento_header['small_image_label']] = $roes1['alt'];
- $products[$roes1['id']][$magento_header['image']] = getImageImportName($roes1['image_path']);
- $products[$roes1['id']][$magento_header['image_label']] = $roes1['alt'];
- $products[$roes1['id']]['media'][] = array(
- '_media_attribute_id' => $_media_attribute_id,
- '_media_image' => getImageImportName($roes1['image_path']),
- '_media_lable' => $roes1['alt'],
- '_media_position' => $roes1['orderby'],
- '_media_is_disabled' => ($roes1['avail'] == 'Y')?0:1,
- );
- }
- $a = $connection->query("select image_path, id, alt, orderby, avail from xcart_images_d");
- $_media_attribute_id = 88;
- while ($roes1 = $a->fetch(PDO::FETCH_ASSOC)) {
- if (!isset($products[$roes1['id']])) {
- continue;
- }
- $products[$roes1['id']]['media'][] = array(
- '_media_attribute_id' => $_media_attribute_id,
- '_media_image' => getImageImportName($roes1['image_path']),
- '_media_lable' => $roes1['alt'],
- '_media_position' => $roes1['orderby'],
- '_media_is_disabled' => ($roes1['avail'] == 'Y')?0:1,
- );
- }
- function sortproducts($a, $b)
- {
- global $magento_header;
- if ($a[$magento_header['_type']] == 'simple') {
- if ($b[$magento_header['_type']] == 'simple') {
- return 0;
- } else {
- return -1;
- }
- } else {
- if ($b[$magento_header['_type']] == 'simple') {
- return 1;
- } else {
- return 0;
- }
- }
- }
- print_r($magento_header);
- usort($products, "sortproducts");
- $globali = $counter = 0;
- $mag_fd = fopen($path . sprintf($magento_file_counter, $globali), "w+b");
- fputcsv($mag_fd, $magento_header_arr);
- foreach ($products as $import_product) {
- //print_r($import_product);
- $counter++;
- if ($counter >= 100000) {
- $counter = 0;
- $globali++;
- fclose($mag_fd);
- $mag_fd = fopen($path . sprintf($magento_file_counter, $globali), "w+b");
- fputcsv($mag_fd, $magento_header_arr);
- }
- $categories = array();
- if (isset($import_product['categories'])) {
- $categories = $import_product['categories'];
- unset($import_product['categories']);
- }
- $websites = array();
- if (isset($import_product['websites'])) {
- $websites = $import_product['websites'];
- unset($import_product['websites']);
- }
- $media = array();
- if (isset($import_product['media'])) {
- $media = $import_product['media'];
- unset($import_product['media']);
- }
- $conf_attributes = array();
- if (isset($import_product['conf_attributes'])) {
- $conf_attributes = array_values($import_product['conf_attributes']);
- unset($import_product['conf_attributes']);
- }
- $conf_skus = array();
- if (isset($import_product['conf_skus'])) {
- $conf_skus = $import_product['conf_skus'];
- unset($import_product['conf_skus']);
- }
- $conf_attributes_options = array();
- if (isset($import_product['conf_attributes_options'])) {
- $conf_attributes_options = $import_product['conf_attributes_options'];
- unset($import_product['conf_attributes_options']);
- }
- $options = array();
- if (isset($import_product['options'])) {
- foreach ($import_product['options'] as $ocode => $odata) {
- $is_first = true;
- foreach ($odata['values'] as $ovalue) {
- $_opt = array();
- if ($is_first) {
- $_opt['_custom_option_type'] = 'drop_down';
- $_opt['_custom_option_title'] = $odata['name'];
- $_opt['_custom_option_is_required'] = 1;
- $_opt['_custom_option_sort_order'] = $odata['position'];
- }
- $is_first = false;
- $_opt['_custom_option_row_title'] = $ovalue;
- $_opt['_custom_option_row_price'] = '0.0000';
- $_opt['_custom_option_row_sort'] = 0;
- $options[] = $_opt;
- }
- /*$options[] = array(
- '_custom_option_store' => 'default',
- '_custom_option_row_title' => $ovalue
- );*/
- }
- if (count($options)) {
- $import_product[$magento_header['has_options']] = 1;
- $import_product[$magento_header['required_options']] = 1;
- }
- unset($import_product['options']);
- }
- for ($i = 0; $i < max(count($categories), count($websites), count($options), count($media), count($conf_attributes), count($conf_skus)); $i++) {
- $row = $import_product;
- if ($i) {
- $row = $empty_magento_line;
- }
- if (isset($websites[$i])) {
- $row[$magento_header['_product_websites']] = $websites[$i];
- }
- if (isset($categories[$i])) {
- $row[$magento_header['_category']] = $categories[$i];
- }
- if (isset($conf_attributes[$i])) {
- $row[$magento_header['_super_attribute_code']] = $conf_attributes[$i];
- }
- if (isset($conf_skus[$i])) {
- $row[$magento_header['_super_products_sku']] = $conf_skus[$i];
- }
- if (isset($conf_attributes_options[$i])) {
- $row[$magento_header['_super_attribute_option']] = $conf_attributes_options[$i];
- }
- if (isset($options[$i])) {
- foreach ($options[$i] as $ko => $kv) {
- $row[$magento_header[$ko]] = $kv;
- }
- }
- if (isset($media[$i])) {
- foreach ($media[$i] as $ko => $kv) {
- $row[$magento_header[$ko]] = $kv;
- }
- }
- if ($row[$magento_header['_type']] == 'configurable') {
- //print_r($row);
- }
- fputcsv($mag_fd, $row);
- }
- }
- fclose($mag_fd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement