`
kirenenko04
  • 浏览: 152222 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

import two option simple - config product

 
阅读更多

这个比较麻烦。有两个可配置想

例如一种鞋子有(尺寸: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');
			}
			
			
		}
		
	}

 

 

分享到:
评论

相关推荐

    uni-simple-router实现简单的路由守卫

    本示例“uni-simple-router实现简单的路由守卫”旨在教你如何利用uni-simple-router库来实现这一功能。uni-simple-router是专为uni-app设计的一个轻量级路由管理插件,它提供了一些强大的特性,包括路由守卫,使我们...

    simple-xml jar包

    **简单XML库(Simple-XML)详解** 在Java开发中,XML作为一种标准的数据交换格式,被广泛用于存储和传输数据。然而,XML的解析和序列化通常需要借助于专门的库来实现,其中Simple-XML就是一个高效且易于使用的库。...

    前端开源库-eslint-config-airbnb-typescript

    `eslint-config-airbnb-typescript`则是`ESLint`的一个配置集,它扩展了著名的`Airbnb`编码风格指南,特别针对`TypeScript`进行了优化。 `Airbnb`是一家知名的创业公司,它的开源代码风格指南在前端社区广泛使用。`...

    vue-two-simple-icons:简单图标作为Vue组件

    "vue-two-simple-icons"是一个专门为Vue设计的组件库,它提供了简洁、易用的图标解决方案。这个库允许开发者将图标集成到Vue项目中,以实现更加直观和美观的界面设计。 Vue.js是一个轻量级的前端JavaScript框架,它...

    slf4j-simple-1.5.2.jar

    SLF4J(Simple Logging Facade for Java)是Java中的一种日志门面,它提供了一个接口层,允许用户在运行时插入所需的日志框架。SLF4J的设计目标是为各种日志库提供一个简单统一的API,使得开发者可以在不修改代码的...

    由 simple-uploader.js 提供支持的 Vue.js 上传组件.zip

    由 simple-uploader.js 提供支持的 Vue.js 上传组件vue-simple-uploader 由simple-uploader.js提供支持的 Vue.js 上传组件使用 vue 3English 中文特征将文件夹和文件视为File暂停/恢复上传恢复上传错误处理使用...

    eslint-plugin-simple-import-sort:易于自动修复的导入排序

    eslint插件简单导入排序易于自动修复的导入排序。 :check_mark_button: 通过eslint --fix运行–无新工具 :check_mark_button: 尽可能对出口进行排序 :check_mark_button: 处理评论 :...import styles from "./styles.cs

    eslint-config-typescript-react:Type为TypeScript React钻KongESLint配置-@boringcodes

    yarn add @boringcodes/eslint-config-typescript-react @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier eslint-config-standard-with-typescript eslint-plugin-import ...

    eslint-config-semistandard:

    eslint-config-semistandard eslint可共享的–类似于 ,但带有分号 安装 使用 : npx install-peerdeps --dev eslint-config-semistandard 或经典方式: npm install --save-dev eslint-plugin-promise eslint-...

    eslint-config-standard-with-typescript:eslint-config-standard的扩展,用于TypeScript

    用法npm install --save-dev eslint@7 eslint-plugin-promise@4 eslint-plugin-import@2 eslint-plugin-node@11 @typescript-eslint/eslint-plugin@4 eslint-config-standard-with-typescript是的,这是很多软件包。...

    Python库 | mypy-boto3-config-1.10.40.1.tar.gz

    《深入解析mypy-boto3-config库:强化Python与AWS的静态类型检查》 在Python开发领域,提高代码质量和可维护性是至关重要的。而mypy-boto3-config库正是一款针对AWS Boto3库的静态类型检查工具,旨在帮助开发者在...

    前端开源库-rollup-config-external-dependencies

    import external from 'rollup-config-external-dependencies'; export default { input: 'src/index.js', output: { file: 'dist/bundle.js', format: 'iife' }, external: external() }; ``` 这里,`...

    ng-config-firebase-remote-config:@DagonMetric ng-config的Angular Firebase远程配置集成

    NG-CONFIG的Angular Firebase远程配置提供程序 针对Angular应用程序的Google Firebase... import { ConfigModule } from '@dagonmetric/ng-config' ; import { FirebaseRemoteConfigProviderModule } from '@dagonmet

    slf4j-simple-1.7.25.rar

    SLF4J(Simple Logging Facade for Java)是Java中的一种日志门面(Logging Facade),它为各种日志框架提供一个简单的统一接口,使得最终用户能够在部署时插入所需的日志实现。SLF4J的主要目标是允许最终用户在部署...

    solr-import-export-json最新代码

    solr-import-export-json最新代码solr-import-export-json最新代码solr-import-export-json最新代码solr-import-export-json最新代码solr-import-export-json最新代码solr-import-export-json最新代码solr-import-...

    neo4j-admin import导入工具csv导入及参数说明

    - 示例:`--additional-config=<config-file-path>` - 默认为空。 - 解释:用于指定一个包含更多配置选项的文件路径。 #### 示例 以下是一个完整的示例命令,展示了如何使用`neo4j-admin import`命令: ```sh ...

    electron-json-config:只需为您的Electron应用设置并从json文件中获取配置

    电子json配置 只需为您的Electron应用设置并从json文件中获取配置这是2.xx树。 有关1.xx代码和文档,请参阅。... // barES模块import { factory } from 'electron-json-config' ;const config = fact

    前端开源库-react-simple-list

    import SimpleList from 'react-simple-list'; const dataList = [/* your data array */]; const ListItem = ({ item }) => ( <div key={item.id}>{item.name} ); function MyList() { return ( <SimpleList ...

    eslint-config-airbnb-typescript:支持TypeScript的Airbnb的ESLint配置

    eslint-config-airbnb-typescript 支持TypeScript的Airbnb的ESLint配置 设置 1)安装 npm install eslint-config-airbnb-typescript --save-dev 2)安装ESLint插件 此配置使用的ESLint插件也必须安装在您的项目中。...

    eslint-config-jd:一套基于eslint-config-airbnb-base的eslint代码规范配置

    eslint-config-jd简介本项目以eslint-config-airbnb-base为基础,定制出了符合团队现状的ESLint 配置。添加的规则 { 'no-restricted-syntax': ['off', "BinaryExpression[operator = 'in']"], // don't require .vue...

Global site tag (gtag.js) - Google Analytics