1. create simple product function:
public function import_simple_product() { $sql = "select * from bysoft_import_product"; $results = $this->_read->fetchAll($sql); $configurable_attribute = "size"; $attr_id = 134; foreach ($results as $data) { //check dropdown attribute exists option or not and add . if ($data['model_filter']) { $model_filter = $this->addAttributeValue('model_filter',$data['model_filter']); } if ($data['color']) { $color = $this->addAttributeValue('color',$data['color']); } if ($data['size']) { $size = $this->addAttributeValue('size',$data['size']); } if ($data['usage']) { $usage = $this->addAttributeValue('usage',$data['usage']); } if ($data['edition']) { $edition = $this->addAttributeValue('edition',$data['edition']); } if ($data['specific_usage']) { $specific_usage = $this->addAttributeValue('specific_usage',$data['specific_usage']); } if ($data['style']) { $style = $this->addAttributeValue('style',$data['style']); } $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($data['attribute_set_id']) ->setCategoryIds($this->getCategoryIds($data)) // Populated further up the script ->setSku($data['config_sku'] . '-' . $data['size']) // $main_product_data is an array created as part of a wider foreach loop, which this code is inside of ->setName($data['cn_name']) ->setData('cn_name',$data['cn_name']) ->setData('en_name',$data['en_name']); if ($data['style']) { $sProduct->setData('style',$style); } if ($data['model_filter']) { $sProduct->setData('model_filter',$model_filter); } if ($data['color']) { $sProduct->setData('color',$color); } if ($data['size']) { $sProduct->setData('size',$size); } if ($data['usage']) { $sProduct->setData('usage',$usage); } if ($data['edition']) { $sProduct->setData('edition',$edition); } if ($data['specific_usage']) { $sProduct->setData('specific_usage',$specific_usage); } $sProduct->setData('price',$data['price']); $sProduct->setStockData(array('is_in_stock' =>1,'qty' => 99999)); $sProduct->save(); var_dump('import ' . $sProduct->getSku() . ' successed!'); } }
2. import config product function
public function import_config_product() { $sql = "SELECT `config_sku` FROM `bysoft_import_product` GROUP BY `config_sku` "; $results = $this->_read->fetchAll($sql); $attr_id = $this->getAttIdByCode('size'); foreach ($results as $row) { $config_sku = $row['config_sku']; $simpleProducts = array(); $sql = "SELECT * FROM bysoft_import_product WHERE config_sku = ?"; $results_simple = $this->_read->fetchAll($sql,array($config_sku)); foreach ($results_simple as $rs) { $sProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $rs['config_sku'] . '-' . $rs['size']); $sProduct = Mage::getModel('catalog/product')->load($sProduct->getId()); array_push( $simpleProducts, array( "id" => $sProduct->getId(), "price" => $sProduct->getPrice(), "attr_code" => 'size', "attr_id" => $attr_id, // i have used the hardcoded attribute id of attribute size, you must change according to your store "value" => $sProduct->getSize(), "label" => $sProduct->getAttributeText('size'), ) ); } $cProduct = Mage::getModel('catalog/product'); $config_sql = "select * from bysoft_import_product where config_sku =? limit 1"; $res_config = $this->_read->fetchAll($config_sql,array($config_sku)); //check dropdown attribute exists option or not and add . foreach ($res_config as $rc) { if ($rc['model_filter']) { $model_filter = $this->addAttributeValue('model_filter',$rc['model_filter']); } if ($rc['color']) { $color = $this->addAttributeValue('color',$rc['color']); } if ($rc['size']) { $size = $this->addAttributeValue('size',$rc['size']); } if ($rc['usage']) { $usage = $this->addAttributeValue('usage',$rc['usage']); } if ($rc['edition']) { $edition = $this->addAttributeValue('edition',$rc['edition']); } if ($rc['specific_usage']) { $specific_usage = $this->addAttributeValue('specific_usage',$rc['specific_usage']); } if ($rc['style']) { $style = $this->addAttributeValue('style',$rc['style']); } $cProduct ->setTypeId('configurable') ->setWebsiteIds(array(1)) ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED) ->setVisibility(4) ->setAttributeSetId($rc['attribute_set_id']) ->setCategoryIds($this->getCategoryIds($rc)) // Populated further up the script ->setSku($rc['config_sku']) // $main_product_data is an array created as part of a wider foreach loop, which this code is inside of ->setName($rc['cn_name']) ->setData('cn_name',$rc['cn_name']) ->setData('en_name',$rc['en_name']) ->setData('description',$rc['description']) ->setData('composition',$rc['composition']) ->setData('care_description',$rc['care_description']) ->setData('season',$rc['season']) ->setData('price',$rc['price']); if ($rc['style']) { $cProduct->setData('style',$style); } if ($rc['model_filter']) { $cProduct->setData('model_filter',$model_filter); } if ($rc['color']) { $cProduct->setData('color',$color); } if ($rc['usage']) { $cProduct->setData('usage',$usage); } if ($rc['edition']) { $cProduct->setData('edition',$edition); } if ($rc['specific_usage']) { $cProduct->setData('specific_usage',$specific_usage); } $cProduct->setCanSaveConfigurableAttributes(true); $cProduct->setCanSaveCustomOptions(true); $_attributeIds = array("size" =>$attr_id); $cProductTypeInstance = $cProduct->getTypeInstance(); $cProductTypeInstance->setUsedProductAttributeIds(array($_attributeIds["size"])); $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, "pricing_value" => $simpleArray['price'] ) ); } } var_dump('simple data:'); var_dump($dataArray); $cProduct->setConfigurableProductsData($dataArray); $cProduct->setStockData( array( 'use_config_manage_stock' => 1, 'is_in_stock' => 1, 'is_salable' => 1 )); $cProduct->save(); var_dump($cProduct->getSku() . ' import success!'); } } }
相关推荐
标题中的“Programmatically Setting Control Adapters for URL Rewriting and AJAX”指的是在编程环境中设置用于URL重写和AJAX的控件适配器的技术。URL重写是改变URL结构以优化SEO(搜索引擎优化)和提高用户体验的...
Key Features ...Create a dexterity for programmatically reading, auditing, correcting, and shaping data. Write and complete programs for taking in, formatting and outputting datasets.
Readers will learn how to set up a Go development environment, create simple programs, and understand the language's unique features such as goroutines and channels. This foundational knowledge is ...
Chapter 1, Sockets, IPv4, and Simple Client/Server Programming, introduces you to Python's core networking library with various small tasks and enables you to create your first clientserver ...
This includes enabling, disabling, and updating features programmatically. - **Managing Permission Levels and Security Groups**: PowerShell can be used to create permission levels and security groups...
* Create clickable GUIs to handle large and complex utilities, * Monitor large clusters of machines by interacting with SNMP programmatically, * Master the IPython Interactive Python shell to replace...
Animation is a core feature in both WPF and Silverlight, enabling developers to create dynamic and engaging user experiences. Both platforms support a variety of animation types, including property ...
It describes strategies and approaches for extracting data from HTML, XML, and JSON formats and how to programmatically access data from the Web. Along with these general skills, the authors ...
It then goes over the construction of very simple networks both programmatically (using NetworkX) and interactively (in Gephi), and it concludes by presenting a network of Wikipedia pages related to ...
The "Hello World" example serves as a starting point for understanding how to create simple GUI applications using Perl/Tk. This includes creating a window, adding text, and handling basic user ...
This book provides you with a technical foundation to implement animation in a way that’s both visually stunning and programmatically maintainable. Throughout, we consider the balance between ...
Begin working with Windows Workflow Foundation to create scalable and durable business services Implement service discovery and message routing Optimize performance with service throttling, encoding, ...
2. **Outlook Object Model**: The Outlook Object Model is a comprehensive set of objects and properties that developers can use to access and manipulate Outlook data programmatically. 3. **Add-ins and...
Geoprocessing is the science of reading, analyzing, and presenting geospatial data programmatically. The Python language, along with dozens of open source libraries and tools, makes it possible to ...
The Win32 API is a set of programming interfaces that provides direct access to the underlying operating system functionalities, enabling developers to create robust and efficient applications for ...
Create drawables programmatically and get rid of the boring and always repeated drawable.xml files. Features Create drawables programmatically Support ,, ,, `` drawables Support 'flip' (vertical...
This library allows to create simple popup menus programmatically with a nice type-safe builder syntax in Kotlin. Menus can be divided into separate sections with optional headers and contain icons. ...