这个比较麻烦。有两个可配置想
例如一种鞋子有(尺寸:35,36,37,和颜色:红,黄,蓝)
那么它就有3*3=9种简单商品,还有一个可配置商品
public function importDoubleMixProduct() { //获取这种类型的商品ID; $read= Mage::getSingleton('core/resource')->getConnection('core_read'); $sql = "SELECT product_id,COUNT(*) FROM (SELECT product_id,option_id FROM `old_product_option_value` GROUP BY product_id,option_id) AS t1 GROUP BY product_id HAVING COUNT(*) = 2 "; $results = $read->fetchAll($this->cleanSql($sql)); foreach ($results as $row) { $product_id = (int)$row['product_id']; $options = array(); $att_ids= array(); $simpleProducts = array(); $sql_option_name = "SELECT `option_name` FROM`old_product_option_value` WHERE product_id = {$product_id} GROUP BY option_name"; $results_option_name = $read->query($sql_option_name); foreach ($results_option_name as $ron) { $option_name = $ron['option_name']; $sql_option_value_name = " SELECT `option_value_name` FROM `old_product_option_value` WHERE `product_id` = ? AND option_name = ? GROUP BY option_value_name "; $results_option_value_name = $read->query($this->cleanSql($sql_option_value_name),array($product_id,$option_name)); foreach ($results_option_value_name as $rovn) { $options[$option_name][] = $rovn['option_value_name']; } } //获取所有option信息 var_dump($options); $option_names = array();//0是第一套属性,1是第二套属性。 foreach ($options as $option_name=>$option_values) { $option_names[] = $option_name; } foreach ($options[$option_names[0]] as $key=>$val) { //第一套属性循环 $attribute_code_1 = $this->getAttributeCode($option_names[0]); $option_value_1 = $this->getOptionId($attribute_code_1, $val); $att_id_1 = $this->getAttributeId($option_names[0]); if (!in_array($att_id_1,$att_ids)) { $att_ids[] = $att_id_1; } //寻找第二套属性 foreach ($options[$option_names[1]] as $key2=>$val2) { $attribute_code_2 = $this->getAttributeCode($option_names[1]); $option_value_2 = $this->getOptionId($attribute_code_2, $val2); $att_id_2 = $this->getAttributeId($option_names[1]); if (!in_array($att_id_2,$att_ids)) { $att_ids[] = $att_id_2; } //导入简单商品 $simple_sku = $product_id . '_' . $key.'_' . $key2; var_dump($simple_sku); //保存简单商品 //获取产品名 $sql = "select * from `old_data_import` WHERE `mid` = ? and not `html` is null and type = 3 limit 1"; $product_result = $read->query($sql,array($product_id)); foreach ($product_result as $row_p) { $cateIds = $this->getCategoryIds($product_id); //get product name $html = new simple_html_dom(); $html->load($row_p['html']); $ret = $html->find('h1.productTitle'); $product_name = ''; if (count($ret)) { foreach ($ret as $e) { $product_name = trim($e->innertext); break; } } var_dump('name:' . $product_name); //get product shortdescription $ret_short_desc = $html->find('span.productShortDescription'); $product_short_desc = ''; if (count($ret_short_desc)) { foreach ($ret_short_desc as $e_short_desc) { $product_short_desc= trim($e_short_desc->innertext); break; } } var_dump('Short Desc:' . $product_short_desc); //get product description $ret_desc = $html->find('div.productLongDescription'); $product_desc = ''; if (count($ret_desc)) { foreach ($ret_desc as $e_desc) { $product_desc= trim($e_desc->innertext); break; } } var_dump('Desc:' . $product_desc); //get product price $product_price = 0; $sql_price = "select price from old_data_price where product_id=? limit 1"; $results_price = $read->query($sql_price,array($row_p['mid'])); foreach ($results_price as $data_price) { $product_price = (float)$data_price['price']; } var_dump('Price:' . $product_price); } $sProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$simple_sku); if (!$sProduct) { $sProduct = Mage::getModel('catalog/product'); $sProduct ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) ->setWebsiteIds(array(1)) ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED) ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE) ->setAttributeSetId(4) ->setCategoryIds($cateIds) // Populated further up the script ->setSku($simple_sku) // $main_product_data is an array created as part of a wider foreach loop, which this code is inside of ->setName($product_name) ->setShortDescription($product_short_desc) ->setDescription($product_desc) ->setPrice(sprintf("%0.2f", $product_price)) ->setData($attribute_code_1, $option_value_1) ->setData($attribute_code_2, $option_value_2) ; // Set the stock data. Let Magento handle this as opposed to manually creating a cataloginventory/stock_item model.. $sProduct->setStockData(array( 'is_in_stock' =>1, 'qty' => 99999 )); $sProduct->save(); $simpleProducts[$sProduct->getId()][] = array( "id" =>$sProduct->getId(), "price" =>$sProduct->getPrice(), "attr_code" =>$attribute_code_1, "attr_id" =>$att_id_1, // i have used the hardcoded attribute id of attribute size, you must change according to your store "value" =>$option_value_1, "label" =>$val ); $simpleProducts[$sProduct->getId()][]= array( "id" =>$sProduct->getId(), "price" =>$sProduct->getPrice(), "attr_code" =>$attribute_code_2, "attr_id" =>$att_id_2, // i have used the hardcoded attribute id of attribute size, you must change according to your store "value" =>$option_value_2, "label" =>$val2, ); $productIds[]=$sProduct->getId(); var_dump($sProduct->getSku() . ' saved'); } } } //创建可配置商品 //save config product $cProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',"c_" . $product_id); if (!$cProduct) { $cProduct = Mage::getModel('catalog/product'); $cProduct ->setTypeId('configurable') ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED) ->setWebsiteIds(array(1)) ->setCategoryIds($cateIds) ->setAttributeSetId(4) ->setSku("c_" . $product_id) ->setName($product_name) ->setShortDescription($product_short_desc) ->setDescription($product_desc) ->setPrice(sprintf("%0.2f", $product_price)) ->setUrlKey($product_id) ; // Set stock data. Yes, it needs stock data. No qty, but we need to tell it to manage stock, and that it's actually // in stock, else we'll end up with problems later.. $cProduct->setStockData(array( 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 0, 'use_config_manage_stock' => 0 )); $cProduct->setCanSaveConfigurableAttributes(true); $cProduct->setCanSaveCustomOptions(true); $cProductTypeInstance = $cProduct->getTypeInstance(); $attribute_ids = $att_ids; $cProductTypeInstance->setUsedProductAttributeIds($attribute_ids); $attributes_array = $cProductTypeInstance->getConfigurableAttributesAsArray(); foreach($attributes_array as $key => $attribute_array) { $attributes_array[$key]['use_default'] = 1; $attributes_array[$key]['position'] = 0; if (isset($attribute_array['frontend_label'])) { $attributes_array[$key]['label'] = $attribute_array['frontend_label']; } else { $attributes_array[$key]['label'] = $attribute_array['attribute_code']; } } // Add it back to the configurable product.. $cProduct->setCanSaveConfigurableAttributes(1); $cProduct->setConfigurableAttributesData($attributes_array); $dataArray = array(); foreach ($simpleProducts as $simple_product_id=>$simpleArray) { foreach ($simpleArray as $attrArray) { $dataArray[$simple_product_id][] = array( "attribute_id" => $attrArray['attr_id'], "label" => $attrArray['label'], "is_percent" => false, "value_index" => $attrArray['value'], "pricing_value" => $product_price ); } } var_dump($dataArray); $cProduct->setConfigurableProductsData($dataArray); $cProduct->save(); // Finally...! var_dump($cProduct->getSku().' data import success'); //import product images //get images; $ret_main_images = $html->find('a.MagicZoom'); $images = array(); if (count($ret_main_images)) { foreach ($ret_main_images as $main_e) { $images[]=trim($main_e->href); } } //get additionnal images $ret_add_images = $html->find('div.productAdditionalImages div.productAdditionalImage img'); if (count($ret_add_images)) { foreach ($ret_add_images as $add_e) { $images[] = trim($add_e->src); } } $this->importProductImages($images,$cProduct->getSku()); var_dump($cProduct->getSku() . ' image import successed'); } } }