Advertisement
johncarlson21

Xcart->Magento import product script

Feb 4th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.89 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "app/Mage.php";
  4. Mage::app();
  5.  
  6. $magento_file = "magento_products.csv";
  7. $magento_file_counter = "magento_products_%s.csv";
  8.  
  9. $websites = array('base');
  10. $_attribute_set = 'Default';
  11. $options_container = 'Block after Info Column';
  12.  
  13. $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');
  14. $magento_header = array();
  15. foreach ($magento_header_arr as $k => $v) {
  16.     $magento_header[$v] = $k;
  17. }
  18.  
  19. $convert_header = array(
  20.     '!PRODUCT' => 1,
  21.     '!DESCR' => 1,
  22.     '!FULLDESCR' => 1,
  23.     '!TITLE_TAG' => 1,
  24.     '!META_KEYWORDS' => 1,
  25.     '!META_DESCRIPTION' => 1,
  26. );
  27. $association = array(
  28.     '!PRODUCTCODE' => 'sku',
  29.     '!PRODUCT' => 'name',
  30.     '!CLEAN_URL' => 'url_key',
  31.     '!WEIGHT' => 'weight',
  32.     '!LIST_PRICE' => 'price',
  33.     '!DESCR' => 'short_description',
  34.     '!FULLDESCR' => 'description',
  35.     '!TITLE_TAG' => 'meta_title',
  36.     '!META_KEYWORDS' => 'meta_keyword',
  37.     '!META_DESCRIPTION' => 'meta_description',
  38.     '!AVAIL' => 'qty',
  39.     '!LOW_AVAIL_LIMIT' => 'min_qty',
  40.     '!MIN_AMOUNT' => 'min_sale_qty',
  41.     '!PRICE' => 'special_price',
  42.     '!MANUFACTURER' => 'turnkeye_brand',
  43. );
  44.  
  45. $empty_magento_line = array();
  46. foreach ($magento_header_arr as $k => $v) {
  47.     $empty_magento_line[$k] = '';
  48. }
  49.  
  50. $product_magento_line = $empty_magento_line;
  51. $product_magento_line[$magento_header['_attribute_set']] = $_attribute_set;
  52. $product_magento_line[$magento_header['enable_googlecheckout']] = 1;
  53. $product_magento_line[$magento_header['_type']] = "simple";
  54. $product_magento_line[$magento_header['has_options']] = 0;
  55. $product_magento_line[$magento_header['required_options']] = 0;
  56. $product_magento_line[$magento_header['tax_class_id']] = 2;
  57. $product_magento_line[$magento_header['visibility']] = 4;
  58.  
  59. $product_magento_line[$magento_header['use_config_min_qty']] = 1;
  60. $product_magento_line[$magento_header['is_qty_decimal']] = 0;
  61. $product_magento_line[$magento_header['backorders']] = 0;
  62. $product_magento_line[$magento_header['use_config_backorders']] = 1;
  63. $product_magento_line[$magento_header['min_sale_qty']] = '0.0000';
  64. $product_magento_line[$magento_header['use_config_min_sale_qty']] = 1;
  65. $product_magento_line[$magento_header['max_sale_qty']] = '0.0000';
  66. $product_magento_line[$magento_header['use_config_max_sale_qty']] = 1;
  67. $product_magento_line[$magento_header['is_in_stock']] = 1;
  68. $product_magento_line[$magento_header['use_config_notify_stock_qty']] = 1;
  69. $product_magento_line[$magento_header['manage_stock']] = 0;
  70. $product_magento_line[$magento_header['use_config_manage_stock']] = 1;
  71. $product_magento_line[$magento_header['stock_status_changed_auto']] = 0;
  72. $product_magento_line[$magento_header['use_config_qty_increments']] = 1;
  73. $product_magento_line[$magento_header['qty_increments']] = '0.0000';
  74. $product_magento_line[$magento_header['use_config_enable_qty_inc']] = 1;
  75. $product_magento_line[$magento_header['enable_qty_increments']] = 0;
  76. $product_magento_line[$magento_header['is_decimal_divided']] = 1;
  77.  
  78.  
  79. $product_magento_line['websites'] = $websites;
  80. $product_magento_line['options'] = array();
  81. $product_magento_line['media'] = array();
  82. $product_magento_line['conf_attributes'] = array();
  83. $product_magento_line['conf_skus'] = array();
  84.  
  85.  
  86. $products_file = "xcart_products.csv";
  87. $option_file = "xcart_product_options.csv";
  88. $variant_file = "xcart_product_variants.csv";
  89.  
  90. $path = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  91. $fd = fopen($path . $products_file, "rb");
  92. $delimiter = ',';
  93.  
  94. $header_arr = fgetcsv($fd, 10000, $delimiter);
  95. $header = array();
  96. foreach ($header_arr as $k => $v) {
  97.     $header[$v] = $k;
  98. }
  99.  
  100. $products = array();
  101. $prev_id = null;
  102. $used_sku = array();
  103. while($row = fgetcsv($fd, 40960, $delimiter)) {
  104.     if ($row[$header['!PRODUCTID']]) {
  105.         $prev_id = $row[$header['!PRODUCTID']];
  106.         $new_row = $product_magento_line;
  107.         foreach ($association as $xcart_field => $magento_field) {
  108.             if (isset($convert_header[$xcart_field])) {
  109.                 $new_row[$magento_header[$magento_field]] = mb_convert_encoding($row[$header[$xcart_field]], "UTF8");
  110.             } else {
  111.                 $new_row[$magento_header[$magento_field]] = $row[$header[$xcart_field]];
  112.             }
  113.         }
  114.  
  115.         if (isset($used_sku[strtolower($row[$header['!PRODUCTCODE']])])) {
  116.             $new_row[$magento_header['sku']] = $new_row[$magento_header['sku']].  "-" . $used_sku[strtolower($row[$header['!PRODUCTCODE']])];
  117.             $used_sku[strtolower($row[$header['!PRODUCTCODE']])]++;
  118.         } else {
  119.             $used_sku[strtolower($row[$header['!PRODUCTCODE']])] = 1;
  120.         }
  121.         $new_row['categories'] = array();
  122.         $new_row['categories'][] = $row[$header['!CATEGORY']];
  123.         $new_row[$magento_header['meta_description']] = Mage::helper('core/string')->substr(Mage::helper('core/string')->cleanString($new_row[$magento_header['meta_description']]), 0, 255);
  124.         if (!$new_row[$magento_header['description']]) {
  125.             if (!$new_row[$magento_header['short_description']]) {
  126.                 $new_row[$magento_header['short_description']] = "No information";
  127.             }
  128.             $new_row[$magento_header['description']] = $new_row[$magento_header['short_description']];
  129.         }
  130.         if ($row[$header['!FORSALE']] == 'Y')  {
  131.             $new_row[$magento_header['status']] = 1;
  132.         } else {
  133.             $new_row[$magento_header['status']] = 2;
  134.         }
  135.         $new_row[$magento_header['created_at']] = date("Y-m-d H:i:s", strtotime($row[$header['!ADD_DATE']]));
  136.         $products[$prev_id] = $new_row;
  137.     } else {
  138.         $products[$prev_id]['categories'][] = $row[$header['!CATEGORY']];
  139.     }
  140. }
  141.  
  142. fclose($fd);
  143. $fd = fopen($path . $option_file, "rb");
  144.  
  145. $header_arr = fgetcsv($fd, 10000, $delimiter);
  146. $header = array();
  147. foreach ($header_arr as $k => $v) {
  148.     $header[$v] = $k;
  149. }
  150.  
  151. $prev_id = null;
  152. $prev_class = null;
  153. $is_option = false;
  154. $prods2toclasses = array();
  155. while($row = fgetcsv($fd, 40960, $delimiter)) {
  156.     if ($row[$header['!PRODUCTID']]) {
  157.         $prev_id = $row[$header['!PRODUCTID']];
  158.     }
  159.  
  160.     if ($row[$header['!CLASS']]) {
  161.         $prev_class = $row[$header['!CLASS']];
  162.         if ($row[$header['!TYPE']] == 'Y') {
  163.             $is_option = true;
  164.         } else {
  165.             $is_option = false;
  166.         }
  167.     }
  168.  
  169.     if ($is_option && isset($products[$prev_id])) {
  170.         if ($row[$header['!CLASS']]) {
  171.             $products[$prev_id]['options'][$prev_class] = array(
  172.                 'name'   => mb_convert_encoding($row[$header['!DESCR']], "UTF8"),
  173.                 'position' => $row[$header['!ORDERBY']],
  174.                 'values' => array(),
  175.             );
  176.         }
  177.  
  178.         $products[$prev_id]['options'][$prev_class]['values'][] = mb_convert_encoding($row[$header['!OPTION']], "UTF8");
  179.     }
  180.  
  181.     if (!isset($prods2toclasses[$prev_id])) {
  182.         $prods2toclasses[$prev_id] = array();
  183.     }
  184.  
  185.     if ($row[$header['!CLASS']]) {
  186.         $prods2toclasses[$prev_id][$row[$header['!CLASS']]] = mb_convert_encoding($row[$header['!DESCR']], "UTF8");
  187.     }
  188. }
  189.  
  190. fclose($fd);
  191.  
  192.  
  193. $fd = fopen($path . $variant_file, "rb");
  194.  
  195. $header_arr = fgetcsv($fd, 10000, $delimiter);
  196. $header = array();
  197. foreach ($header_arr as $k => $v) {
  198.     $header[$v] = $k;
  199. }
  200.  
  201. $code2id = array();
  202. $installer = Mage::getResourceModel('catalog/setup', 'core_setup');
  203. /**
  204.  * @var $connection Varien_Db_Adapter_Interface
  205.  */
  206. $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
  207. $optionTable =  Mage::getSingleton('core/resource')->getTableName('eav/attribute_option');
  208. $optionValueTable =  Mage::getSingleton('core/resource')->getTableName('eav/attribute_option_value');
  209. function getAttributeCodeValue($code, $value)
  210. {
  211.     global $code2id, $installer, $optionTable, $optionValueTable, $connection;
  212.     $value = mb_convert_encoding($value, "UTF8");
  213.     if (!isset($code2id[$code])) {
  214.         $code2id[$code] = $installer->getAttribute('catalog_product', $code, 'attribute_id');
  215.     }
  216.  
  217.     $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) . "");
  218.     $opt_val = $res->fetch(PDO::FETCH_ASSOC);
  219.     if ($opt_val && $opt_val['option_id']) {
  220.         $option_id = $opt_val['option_id'];
  221.     } else {
  222.         $data = array(
  223.             'attribute_id'  => $code2id[$code],
  224.             'sort_order'    => 0,
  225.         );
  226.         $connection->insert($optionTable, $data);
  227.         $magento_id = $connection->lastInsertId();
  228.         $data = array(
  229.             'option_id' => $magento_id,
  230.             'store_id'  => 0,
  231.             'value'     => $value,
  232.         );
  233.         $connection->insert($optionValueTable, $data);
  234.         $option_id = $magento_id;
  235.     }
  236.  
  237.     $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) . "");
  238.     $opt_val = $res->fetch(PDO::FETCH_ASSOC);
  239.  
  240.     if (!isset($opt_val['value'])) {
  241.         echo 'Cannot find value: ' . $value . "\n";
  242.     }
  243.     return $opt_val['value'];
  244. }
  245.  
  246. /**
  247.  * @var $installer Mage_Catalog_Model_Resource_Setup
  248.  */
  249. $used_classes = array();
  250. $used_classes_options = array();
  251. while($row = fgetcsv($fd, 40960, $delimiter)) {
  252.     //print_r($row);
  253.     $prod_id = $row[$header['!PRODUCTID']];
  254.     if (!isset($products[$prod_id])) {
  255.         continue;
  256.     }
  257.  
  258.     $attribute_code = substr(preg_replace("/_+/", "_", preg_replace('/[^a-z0-9]/', '_', trim(strtolower("conf_" . $row[$header['!CLASS']])))), 0, 30);
  259.     $attribute_name = $row[$header['!CLASS']];
  260.     if (isset($prods2toclasses[$prod_id]) && isset($prods2toclasses[$prod_id][$attribute_name])) {
  261.         $attribute_name = $prods2toclasses[$prod_id][$attribute_name];
  262.     }
  263.     if (!isset($used_classes[$attribute_code])) {
  264.         $used_classes[$attribute_code] = $attribute_name;
  265.         if ($installer->getAttribute('catalog_product', $attribute_code)) {
  266.             $code2id[$attribute_code] = $installer->getAttribute('catalog_product', $attribute_code, 'attribute_id');
  267.             continue;
  268.         }
  269.         $installer->addAttribute('catalog_product', $attribute_code, array(
  270.             'label'        => $attribute_name,
  271.             'required'     => false,
  272.             'input'        => 'select',
  273.             'default'      => '',
  274.             'position'     => 1,
  275.             'sort_order'   => 3,
  276.             'group'        => 'General',
  277.             'user_defined' => true
  278.         ));
  279.         $code2id[$attribute_code] = $installer->getAttribute('catalog_product', $attribute_code, 'attribute_id');
  280.     }
  281.     if (!isset($used_classes_options[$attribute_code])) {
  282.         $used_classes_options[$attribute_code] = array();
  283.     }
  284.  
  285.     $used_classes_options[$attribute_code][] = mb_convert_encoding($row[$header['!OPTION']], "UTF8");
  286.  
  287.     if (!isset($magento_header[$attribute_code])) {
  288.         $magento_header[$attribute_code] = count($magento_header);
  289.         $magento_header_arr[$magento_header[$attribute_code]] = $attribute_code;
  290.     }
  291.     $products[$prod_id][$magento_header['_type']] = "configurable";
  292.     $products[$prod_id]['conf_attributes'][$attribute_code] = $attribute_code;
  293.     $simple = $products[$prod_id];
  294.     $simple['conf_attributes'] = array();
  295.     $simple['conf_skus'] = array();
  296.     $simple['conf_attributes_options'] = array();
  297.     $simple['categories'] = array();
  298.     $simple[$magento_header['_type']] = "simple";
  299.     $simple[$magento_header['visibility']] = "1";
  300.     $simple[$magento_header['price']] = $row[$header['!PRICE']];
  301.     $simple[$magento_header['special_price']] = '';
  302.     $simple[$magento_header['qty']] = $row[$header['!AVAIL']];
  303.     $simple[$magento_header['sku']] = $simple[$magento_header['sku']] . "-" . $row[$header['!VARIANTID']];
  304.     foreach ($magento_header_arr as  $mhk => $mhv) {
  305.         if (!isset($simple[$mhk])) {
  306.             $simple[$mhk] = '';
  307.         }
  308.     }
  309.     $simple[$magento_header[$attribute_code]] = getAttributeCodeValue($attribute_code, $row[$header['!OPTION']]);
  310.     $products[$prod_id]['conf_skus'][] = $simple[$magento_header['sku']];
  311.     $products[$prod_id . "-" . $row[$header['!VARIANTID']]] =  $simple;
  312.     if(!empty($simple[$magento_header[$attribute_code]])) {
  313.         $products[$prod_id]['conf_attributes_options'][] = $simple[$magento_header[$attribute_code]];
  314.     }
  315.     //print_r($products[$prod_id]);
  316. }
  317.  
  318. fclose($fd);
  319. /*foreach ($used_classes as $code => $name) {
  320.     if ($installer->getAttribute('catalog_product', $code)) {
  321.         $code2id[$code] = $installer->getAttribute('catalog_product', $code, 'attribute_id');
  322.         continue;
  323.     }
  324.     $installer->addAttribute('catalog_product', $code, array(
  325.         'label'        => $name,
  326.         'required'     => false,
  327.         'input'        => 'select',
  328.         'default'      => '',
  329.         'position'     => 1,
  330.         'sort_order'   => 3,
  331.         'group'        => 'General',
  332.         'user_defined' => true
  333.     ));
  334.     $code2id[$code] = $installer->getAttribute('catalog_product', $code, 'attribute_id');
  335. }*/
  336.  
  337. /*
  338. foreach ($used_classes_options as $code => $values) {
  339.     foreach ($values as $value) {
  340.         $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) . "");
  341.         $opt_val = $res->fetch(PDO::FETCH_ASSOC);
  342.         if ($opt_val['cnt']) {
  343.             continue;
  344.         }
  345.         $data = array(
  346.             'attribute_id'  => $code2id[$code],
  347.             'sort_order'    => 0,
  348.         );
  349.         $connection->insert($optionTable, $data);
  350.         $magento_id = $connection->lastInsertId();
  351.         $data = array(
  352.             'option_id' => $magento_id,
  353.             'store_id'  => 0,
  354.             'value'     => $value,
  355.         );
  356.         $connection->insert($optionValueTable, $data);
  357.     }
  358. }
  359. */
  360.  
  361. function getImageImportName($image)
  362. {
  363.     $image = str_replace("./images/", '/', $image);
  364.     return str_replace("./files/images/", '/', $image);
  365. }
  366.  
  367. $a = $connection->query("select image_path, id, alt, orderby, avail from xcart_images_t");
  368.  
  369. $_media_attribute_id = 88;
  370. while ($roes1 = $a->fetch(PDO::FETCH_ASSOC)) {
  371.     if (!isset($products[$roes1['id']])) {
  372.         continue;
  373.     }
  374.  
  375.     $products[$roes1['id']][$magento_header['thumbnail']] = getImageImportName($roes1['image_path']);
  376.     $products[$roes1['id']][$magento_header['thumbnail_label']] = $roes1['alt'];
  377.     $products[$roes1['id']]['media'][] = array(
  378.         '_media_attribute_id' => $_media_attribute_id,
  379.         '_media_image' => getImageImportName($roes1['image_path']),
  380.         '_media_lable' => $roes1['alt'],
  381.         '_media_position' => $roes1['orderby'],
  382.         '_media_is_disabled' => ($roes1['avail'] == 'Y')?0:1,
  383.     );
  384. }
  385.  
  386. $a = $connection->query("select image_path, id, alt, orderby, avail from xcart_images_p");
  387.  
  388. $_media_attribute_id = 88;
  389. while ($roes1 = $a->fetch(PDO::FETCH_ASSOC)) {
  390.     if (!isset($products[$roes1['id']])) {
  391.         continue;
  392.     }
  393.  
  394.     $products[$roes1['id']][$magento_header['small_image']] = getImageImportName($roes1['image_path']);
  395.     $products[$roes1['id']][$magento_header['small_image_label']] = $roes1['alt'];
  396.     $products[$roes1['id']][$magento_header['image']] = getImageImportName($roes1['image_path']);
  397.     $products[$roes1['id']][$magento_header['image_label']] = $roes1['alt'];
  398.     $products[$roes1['id']]['media'][] = array(
  399.         '_media_attribute_id' => $_media_attribute_id,
  400.         '_media_image' => getImageImportName($roes1['image_path']),
  401.         '_media_lable' => $roes1['alt'],
  402.         '_media_position' => $roes1['orderby'],
  403.         '_media_is_disabled' => ($roes1['avail'] == 'Y')?0:1,
  404.     );
  405. }
  406.  
  407. $a = $connection->query("select image_path, id, alt, orderby, avail from xcart_images_d");
  408.  
  409. $_media_attribute_id = 88;
  410. while ($roes1 = $a->fetch(PDO::FETCH_ASSOC)) {
  411.     if (!isset($products[$roes1['id']])) {
  412.         continue;
  413.     }
  414.  
  415.     $products[$roes1['id']]['media'][] = array(
  416.         '_media_attribute_id' => $_media_attribute_id,
  417.         '_media_image' => getImageImportName($roes1['image_path']),
  418.         '_media_lable' => $roes1['alt'],
  419.         '_media_position' => $roes1['orderby'],
  420.         '_media_is_disabled' => ($roes1['avail'] == 'Y')?0:1,
  421.     );
  422. }
  423.  
  424. function sortproducts($a, $b)
  425. {
  426.     global $magento_header;
  427.     if ($a[$magento_header['_type']] == 'simple') {
  428.         if ($b[$magento_header['_type']] == 'simple') {
  429.             return 0;
  430.         } else {
  431.             return -1;
  432.         }
  433.     } else {
  434.         if ($b[$magento_header['_type']] == 'simple') {
  435.             return 1;
  436.         } else {
  437.             return 0;
  438.         }
  439.     }
  440. }
  441. print_r($magento_header);
  442. usort($products, "sortproducts");
  443. $globali = $counter = 0;
  444. $mag_fd = fopen($path . sprintf($magento_file_counter, $globali), "w+b");
  445. fputcsv($mag_fd, $magento_header_arr);
  446. foreach ($products as $import_product) {
  447.     //print_r($import_product);
  448.     $counter++;
  449.     if ($counter >= 100000) {
  450.         $counter = 0;
  451.         $globali++;
  452.         fclose($mag_fd);
  453.         $mag_fd = fopen($path . sprintf($magento_file_counter, $globali), "w+b");
  454.         fputcsv($mag_fd, $magento_header_arr);
  455.     }
  456.     $categories = array();
  457.     if (isset($import_product['categories'])) {
  458.         $categories = $import_product['categories'];
  459.         unset($import_product['categories']);
  460.     }
  461.  
  462.     $websites = array();
  463.     if (isset($import_product['websites'])) {
  464.         $websites = $import_product['websites'];
  465.         unset($import_product['websites']);
  466.     }
  467.  
  468.     $media = array();
  469.     if (isset($import_product['media'])) {
  470.         $media = $import_product['media'];
  471.         unset($import_product['media']);
  472.     }
  473.  
  474.     $conf_attributes = array();
  475.     if (isset($import_product['conf_attributes'])) {
  476.         $conf_attributes = array_values($import_product['conf_attributes']);
  477.         unset($import_product['conf_attributes']);
  478.     }
  479.  
  480.     $conf_skus = array();
  481.     if (isset($import_product['conf_skus'])) {
  482.         $conf_skus = $import_product['conf_skus'];
  483.         unset($import_product['conf_skus']);
  484.     }
  485.    
  486.     $conf_attributes_options = array();
  487.     if (isset($import_product['conf_attributes_options'])) {
  488.         $conf_attributes_options = $import_product['conf_attributes_options'];
  489.         unset($import_product['conf_attributes_options']);
  490.     }
  491.  
  492.     $options = array();
  493.     if (isset($import_product['options'])) {
  494.         foreach ($import_product['options'] as $ocode => $odata) {
  495.             $is_first = true;
  496.             foreach ($odata['values'] as $ovalue) {
  497.                 $_opt = array();
  498.                 if ($is_first) {
  499.                     $_opt['_custom_option_type'] = 'drop_down';
  500.                     $_opt['_custom_option_title'] = $odata['name'];
  501.                     $_opt['_custom_option_is_required'] = 1;
  502.                     $_opt['_custom_option_sort_order'] = $odata['position'];
  503.                 }
  504.  
  505.                 $is_first = false;
  506.                 $_opt['_custom_option_row_title'] = $ovalue;
  507.                 $_opt['_custom_option_row_price'] = '0.0000';
  508.                 $_opt['_custom_option_row_sort'] = 0;
  509.                 $options[] = $_opt;
  510.             }
  511.  
  512.             /*$options[] = array(
  513.                 '_custom_option_store' => 'default',
  514.                 '_custom_option_row_title' => $ovalue
  515.             );*/
  516.         }
  517.  
  518.         if (count($options)) {
  519.             $import_product[$magento_header['has_options']] = 1;
  520.             $import_product[$magento_header['required_options']] = 1;
  521.         }
  522.  
  523.         unset($import_product['options']);
  524.     }
  525.  
  526.     for ($i = 0; $i < max(count($categories), count($websites), count($options), count($media), count($conf_attributes), count($conf_skus)); $i++) {
  527.         $row = $import_product;
  528.         if ($i) {
  529.             $row = $empty_magento_line;
  530.         }
  531.  
  532.         if (isset($websites[$i])) {
  533.             $row[$magento_header['_product_websites']] = $websites[$i];
  534.         }
  535.  
  536.         if (isset($categories[$i])) {
  537.             $row[$magento_header['_category']] = $categories[$i];
  538.         }
  539.  
  540.         if (isset($conf_attributes[$i])) {
  541.             $row[$magento_header['_super_attribute_code']] = $conf_attributes[$i];
  542.         }
  543.  
  544.         if (isset($conf_skus[$i])) {
  545.             $row[$magento_header['_super_products_sku']] = $conf_skus[$i];
  546.         }
  547.        
  548.         if (isset($conf_attributes_options[$i])) {
  549.             $row[$magento_header['_super_attribute_option']] = $conf_attributes_options[$i];
  550.         }
  551.  
  552.  
  553.         if (isset($options[$i])) {
  554.             foreach ($options[$i] as $ko => $kv) {
  555.                 $row[$magento_header[$ko]] = $kv;
  556.             }
  557.         }
  558.  
  559.         if (isset($media[$i])) {
  560.             foreach ($media[$i] as $ko => $kv) {
  561.                 $row[$magento_header[$ko]] = $kv;
  562.             }
  563.         }
  564.         if ($row[$magento_header['_type']] == 'configurable') {
  565.             //print_r($row);
  566.         }
  567.  
  568.         fputcsv($mag_fd, $row);
  569.     }
  570. }
  571.  
  572. fclose($mag_fd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement