只有一种选项的简单商品和可配置商品
例如:某鞋子有尺寸这一可选项: (尺码: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'); } } }
相关推荐
pycharm的最佳配置文件
Resource file and Excel file import and export Resource file and Excel file import and export Resource file and Excel file import and export
标题中的“Python库 | mds_import_product-4.6.2.tar.gz”表明这是一个Python库,具体名为`mds_import_product`,版本号为4.6.2,并且已打包成`.tar.gz`格式的压缩文件。`.tar.gz`是Linux和Unix系统中常见的归档压缩...
"mmcv中的config模块,摘出来了" 这个标题表明我们要讨论的是一个与mmcv库相关的主题,特别是它的config模块。mmcv是计算机视觉领域的一个开源框架,广泛应用于深度学习模型的训练和推理。config模块是mmcv中用于...
Export-Import DataPump Parameters INCLUDE and EXCLUDE - How to Load and Unload Specific Objects (Doc ID 341733.1)
import logging.config logging.config.fileConfig(path) logger = logging.getLogger('') 利用以上python代码配置日志输出时,如果该脚本是主脚本(即import别人,不被别人import,在执行逻辑的最顶端),path表示...
### neo4j-admin import工具详解:CSV导入及参数说明 #### 概述 `neo4j-admin import` 是Neo4j数据库系统中的一个强大工具,用于将外部数据(如CSV文件)导入到Neo4j图数据库中。此工具提供了一系列灵活的选项来...
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, ...
eslint插件简单导入排序易于自动修复的导入排序。 :check_mark_button: 通过eslint --fix运行–无新工具 :check_mark_button: 尽可能对出口进行排序 :check_mark_button: 处理评论 :...import styles from "./styles.cs
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....
Bruce Eckel的SimpleTest框架包含了一些基本的测试类,如`Test`和`TestDriver`,它们帮助开发者组织和执行测试用例。`Test`类提供了断言(assert)方法,用于检查代码的输出是否符合预期,而`TestDriver`则用于收集...
#!/usr/bin/python # -*- coding: utf-8 -*- ...from config_build_common import build_common_config config_build_common.make_build_common_config(ARGUMENTS) # 引入其它配置,并生成其它配置
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 ...
本文将详细解析这款名为"woocommerce-xml-csv-product-import.1.3.0.zip"的插件,旨在帮助用户更好地理解和利用这一功能强大的工具。 首先,我们来看"woocommerce-xml-csv-product-import"这个描述。这明确地告诉...
复制代码 代码如下:“””project trace system“””import sysimport ConfigParserimport loggingimport logging.configimport warnings if __name__ == “__main__”: log_config_file = “log.conf”
and any protector with Import Elimination, Directly Imports and Shuffled, Disordered, Scattered Imports. Notes: ====== This tool is an Import Fixer (not Import Rebuilder ImpRec etc) and Just work in ...
`import-product` 库则是针对Magento 2的一个重要工具,它专注于产品的导入功能,极大地提升了数据录入和更新的效率。 在Magento 2中,产品是构成商店核心的商品单元,可以是简单产品、组合产品、虚拟产品等。`...
django-simple-import旨在跟踪数据库中的日志和用户首选项。 项目现在稳定并且处于仅维护模式。 如果您想添加功能,请分叉或接管所有权。 变更日志 2.1 添加对Django 2.2和3.0的支持 移至gitlab和gitlab CI以获得...
CODESYS plaintext import and export scripts
**简单XML库(Simple-XML)详解** 在Java开发中,XML作为一种标准的数据交换格式,被广泛用于存储和传输数据。然而,XML的解析和序列化通常需要借助于专门的库来实现,其中Simple-XML就是一个高效且易于使用的库。...