只有一种选项的简单商品和可配置商品
例如:某鞋子有尺寸这一可选项: (尺码:35,36,37,38,39)
主要是先创建5个拥有者写属性值的简单商品
然后创建一个可配置商品。
配置好可配置商品的配置项,将简单商品关联到这个可配置商品
/** * 导入有option value 的简单商品 */ public function importMixProduct() { //获取这种类型的商品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(*) = 1 "; //不在option表里的商品id $model=Mage::getModel('eav/entity_setup','core_setup'); $results = $read->query($sql); foreach ($results as $row) { $product_id = (int)$row['product_id']; //寻找可配置项ID $sql = "select option_id from old_product_option_value where product_id = ? group by option_id"; $results_option_ids = $read->query($sql,array($product_id)); //查找它的简单商品配置 $sql = "select * from old_product_option_value where product_id = ?"; $results_simple = $read->query($sql,array($product_id)); $productIds = array(); $_attributeIds = array(); $att_ids = array(); $simpleProducts = array(); foreach ($results_simple as $key=>$rs) { $simple_sku = $product_id . '_' . $key; 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) { $attribute_code = $this->getAttributeCode($rs['option_name']); $option_id = $this->getOptionId($attribute_code,$rs['option_value_name']); $att_id = $this->getAttributeId($rs['option_name']); if (!in_array($att_id,$att_ids)) { $att_ids[] = $att_id; } $_attributeIds[$attribute_code] = $option_id; $cateIds = $this->getCategoryIds($product_id); var_dump($cateIds); //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, $option_id) ; // 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(); array_push( $simpleProducts, array( "id" =>$sProduct->getId(), "price" =>$sProduct->getPrice(), "attr_code" =>$attribute_code, "attr_id" =>$att_id, // i have used the hardcoded attribute id of attribute size, you must change according to your store "value" =>$option_id, "label" =>$rs['option_value_name'], ) ); $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->setConfigurableAttributesData($attributes_array); $dataArray = array(); foreach ($simpleProducts as $simpleArray) { $dataArray[$simpleArray['id']] = array(); foreach ($attributes_array as $attrArray) { array_push( $dataArray[$simpleArray['id']], array( "attribute_id" => $simpleArray['attr_id'], "label" => $simpleArray['label'], "is_percent" => false, "value_index" => $simpleArray['value'], "pricing_value" => $product_price ) ); } } $cProduct->setConfigurableProductsData($dataArray); $cProduct->setCanSaveConfigurableAttributes(1); var_dump($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'); } } }
相关推荐
Resource file and Excel file import and export Resource file and Excel file import and export Resource file and Excel file import and export
pycharm的最佳配置文件
import logging.config logging.config.fileConfig(path) logger = logging.getLogger('') 利用以上python代码配置日志输出时,如果该脚本是主脚本(即import别人,不被别人import,在执行逻辑的最顶端),path表示...
Import Products categories, multiple images and custom options
https://mmcv.readthedocs.io/zh_CN/latest/understand_mmcv/config.html
Import, visualize, and extract image features from CT and RT Dose DICOM files in MATLAB.zip
Detailed help system and a demo application for a quicker mastering of the product Delphi 5-7, 2005-2007, 2009, 2010, XE-XE8, 10 Seattle, 10.1 Berlin, 10.2 Tokyo and C++ Builder 5-6, 2006-2007, 2009, ...
python库。 资源全名:mds_import_product-4.6.2.tar.gz
Just as a chemist learns how to clean test tubes and stock a lab, you’ll learn how to clean data and draw plots—and many other things besides. These are the skills that allow data science to happen...
import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org....
#!/usr/bin/python # -*- coding: utf-8 -*- ...from config_build_common import build_common_config config_build_common.make_build_common_config(ARGUMENTS) # 引入其它配置,并生成其它配置
woocommerce-xml-csv-product-import
R for Data Science: Import, Tidy, Transform, Visualize, and Model Data ISBN: 1491910399 | 2017 | True PDF | 522 pages | 32 MB What exactly is data science? With this book, you’ll gain a clear ...
_Data Import and Export.pdf
复制代码 代码如下:“””project trace system“””import sysimport ConfigParserimport loggingimport logging.configimport warnings if __name__ == “__main__”: log_config_file = “log.conf”
tslint-import-eslint-config 将您的ESLint配置导入TSLint。 提供了ESLint和TSLint之间的规则映射。 谢谢! 建立 $ npm i -D tslint-import-eslint-config 编辑您的tslint.js const importESLintConfig = ...
Quickly and easily import files by dragging them from your file explorer and dropping them into Blender’s 3D Viewport or Outliner. No more navigating through menus or dialogs – just a seamless and ...
django-simple-import旨在跟踪数据库中的日志和用户首选项。 项目现在稳定并且处于仅维护模式。 如果您想添加功能,请分叉或接管所有权。 变更日志 2.1 添加对Django 2.2和3.0的支持 移至gitlab和gitlab CI以获得...
TQImport3ASCII Import data from CSV and TXT files TADO_QImport3Access Import data from MS Access database TQImport3HTML Intended for importing tables from HTML pages TQImport3XMLDoc Import data from ...
eslint插件简单导入排序易于自动修复的导入排序。 :check_mark_button: 通过eslint --fix运行–无新工具 :check_mark_button: 尽可能对出口进行排序 :check_mark_button: 处理评论 :...import styles from "./styles.cs