import simple product

public function importOnlySimpleProduct() {
		$read= Mage::getSingleton('core/resource')->getConnection('core_read');
		$sql = "SELECT `mid` AS `product_id`
				FROM `old_data_import`
				WHERE NOT `mid`  IN 
					(SELECT `product_id` FROM `old_product_option_value` GROUP BY `product_id`)
				AND `type` = 3 
				AND NOT `html`  IS NULL
				GROUP BY `mid`
				";//不在option表里的商品id
		
		$model=Mage::getModel('eav/entity_setup','core_setup');
		$results = $read->query($sql);
		foreach ($results as $key=>$row) {
			$product_id = $row['product_id'];
			$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) {
				//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 categories
				$cateids = $this->getCategoryIds($product_id);
				
				//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);
				
				// Create the Magento product model
				$sProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',trim((int)$row_p['mid']));
				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_BOTH)
					->setAttributeSetId(4)
					->setCategoryIds($cateids) // Populated further up the script
					->setSku(trim((int)$row_p['mid']))
					// $main_product_data is an array created as part of a wider foreach loop, which this code is inside of
					->setName($product_name)
					->setDescription($product_desc)
					->setShortDescription($product_short_desc)
					->setPrice(sprintf("%0.2f", $product_price))
					;
					
					// 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();
					var_dump($sProduct->getSku() . ' 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,trim((int)$row_p['mid']));
					var_dump($sProduct->getSku(). ' images import success');
				}
				break;
			}
			
		}
	}

 

你可能感兴趣的:(import)