Magento再度发力-使用程序模型(Model)创建商品
我们的业务有个需求是从另一个库中导出数据,然后同步到Magento数据库中。之前同事研究过模拟post提交的方式,可以成功,但是速度很慢。后来同事研究过dataflow导入,发现父子商品无法关联。今晚我要使用模型去创建父子关联商品,此篇文章介绍如何创建父商品、创建子商品及如何关联父子商品。
作者:精东
转载请留名
一、第一个目标,创建父商品。
首先我打段原始数据:
Array ( [pool_id] => 1 [item_parent] => 122083 [item] => 150009417 [ean] => 1400001001072 [item_description] => TESTELC [color_id] => 10115 [color_description] => PAVEMENT [size_id] => M38_0001 [size_description] => 1 [season_id] => 6 [season_description] => Fall [phase_id] => 5 [phase_description] => Basics [weight] => 11.2 [pack_factor] => 24 [unit_weight] => 0.466667 [regular_unit_retail] => 46.99 [selling_retail_currency] => CNY [division_number] => 116 [division_description] => BODY [department_number] => 1166 [department_description] => WOMENS LOUNGE [class_number] => 6 [class_description] => JACKETS [subclass_number] => 1 [subclass_description] => SLVLS JK [zh_item_title] => [zh_color_description] => [zh_romance_copy] => [create_datetime] => 2010-05-05 03:53:33 [last_update_datetime] => 2010-05-19 21:51:24 [is_deleted] => 0 [last_deleted_at] => 0000-00-00 00:00:00 [is_synchronized] => 0 [last_synchronized_at] => 0000-00-00 00:00:00 [status] => 0 )
字段介绍:首先说我是做服装站点的,已经有三年时间。从数据看出item_parent是商品的style,也就是服装的款式,下面还有颜色、尺码等,最小单位是SKU,也就是字段item。
我的目标是首先去通过item去产品表中查询是否有这个商品,如果有则更新,没有则新增,代码:
foreach($items as $item) { $pid = $product->getIdBySku($item->getData('item')); if(!$pid){ $this->createNewProduct($item); }else{ $this->updateProduct($item); } }
接下来写创建商品的方法,结果研究eav_attribute,可以看到以下字段是必添的。

接下来我创建父商品,请看函数createNewProduct:
public function createNewProduct($item) { //create Parent Product $product = G::getModel ( 'catalog/product' ); $defaultAttributeSetId = G::getModel('eav/entity_type')->loadByCode('catalog_product')->getDefaultAttributeSetId(); /* @var $product G_Catalog_Model_Product */ //check if there has parent product $parentId = $product->getIdBySku ( $item->getData ( 'item_parent' ) ); if (! $parentId) { $product->setTypeId ( G_Catalog_Model_Product_Type::TYPE_CONFIGURABLE ); $product->setAttributeSetId ( $defaultAttributeSetId ); $product->setSku ( $item->getData ( 'item_parent' ) ); $product->setHasOptions ( 1 ); $product->setRequiredOptions ( 1 ); $product->setPrice ( $item->getData ( 'regular_unit_retail' ) ); $product->setName ( $item->getData ( 'zh_item_title' ) ? $item->getData ( 'zh_item_title' ) : $item->getData ( 'item_description' ) ); $product->setDescription ( $item->getData ( 'zh_item_description' ) ? $item->getData ( 'zh_item_description' ) : $item->getData ( 'item_description' ) ); $product->setShortDescription ( $item->getData ( 'zh_romance_copy' ) ? $item->getData ( 'zh_romance_copy' ) : $item->getData ( 'item_description' ) ); $product->setTaxClassId ( 0 ); $product->setVisibility ( G_Catalog_Model_Product_Visibility::VISIBILITY_BOTH ); $product->setWeight ( $item->getData ( 'unit_weight' ) * 1000 ); $product->setStatus ( 2 ); $product->setStockData ( array ('is_in_stock' => 1 ) ); $product->setCreatedAt ( strtotime ( 'now' ) ); $product->save (); } else { //create child product } $instanceParent = $product->getTypeInstance (); /* @var $instanceParent G_Catalog_Model_Product_Type_Configurable */ die ( get_class ( $instanceParent ) ); }
需要注意的地方是:
setAttributeSetId,这里默认使用的是4,可以看eav_attribute_set这个表,4是catalog_product。

当然你如果不愿意使用固定数值,你也可以通过模型获得。
$defaultAttributeSetId = G::getModel('eav/entity_type')->loadByCode('catalog_product')->getDefaultAttributeSetId();
OK,写到这里,应该已经可以创建一个父商品了,看看你的数据库,再看看后台的商品列表。应该已经出现了这个父商品。函数中有些凌乱的debug语句,呵呵,下面会删除,这里是调试用。
二、创建子商品,有了父商品的经验,创建子商品相对比较简单。
直接看代码吧:
public function createNewProduct($item) { //create Parent Product $product = G::getModel ( 'catalog/product' ); $defaultAttributeSetId = G::getModel('eav/entity_type')->loadByCode('catalog_product')->getDefaultAttributeSetId(); /* @var $product G_Catalog_Model_Product */ //check if there has parent product $parentId = $product->getIdBySku ( $item->getData ( 'item_parent' ) ); if (! $parentId) { $product->setTypeId ( G_Catalog_Model_Product_Type::TYPE_CONFIGURABLE ); $product->setAttributeSetId ( $defaultAttributeSetId ); $product->setSku ( $item->getData ( 'item_parent' ) ); $product->setHasOptions ( 1 ); $product->setRequiredOptions ( 1 ); $product->setPrice ( $item->getData ( 'regular_unit_retail' ) ); $product->setName ( $item->getData ( 'zh_item_title' ) ? $item->getData ( 'zh_item_title' ) : $item->getData ( 'item_description' ) ); $product->setDescription ( $item->getData ( 'zh_item_description' ) ? $item->getData ( 'zh_item_description' ) : $item->getData ( 'item_description' ) ); $product->setShortDescription ( $item->getData ( 'zh_romance_copy' ) ? $item->getData ( 'zh_romance_copy' ) : $item->getData ( 'item_description' ) ); $product->setTaxClassId ( 0 ); $product->setVisibility ( G_Catalog_Model_Product_Visibility::VISIBILITY_BOTH ); $product->setWeight ( $item->getData ( 'unit_weight' ) * 1000 ); $product->setStatus ( 2 ); $product->setStockData ( array ('is_in_stock' => 1 ) ); $product->setCreatedAt ( strtotime ( 'now' ) ); $product->save (); } else { $product->setTypeId ( G_Catalog_Model_Product_Type::TYPE_SIMPLE ); $product->setAttributeSetId ( $defaultAttributeSetId ); $product->setSku ( $item->getData ( 'item' ) ); $product->setHasOptions ( 1 ); $product->setRequiredOptions ( 1 ); $product->setPrice ( $item->getData ( 'regular_unit_retail' ) ); $product->setName ( $item->getData ( 'zh_item_title' ) ? $item->getData ( 'zh_item_title' ) : $item->getData ( 'item_description' ) ); $product->setDescription ( $item->getData ( 'zh_item_description' ) ? $item->getData ( 'zh_item_description' ) : $item->getData ( 'item_description' ) ); $product->setShortDescription ( $item->getData ( 'zh_romance_copy' ) ? $item->getData ( 'zh_romance_copy' ) : $item->getData ( 'item_description' ) ); $product->setTaxClassId ( 0 ); $product->setVisibility ( G_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE ); $product->setWeight ( $item->getData ( 'unit_weight' ) * 1000 ); $product->setStatus ( 2 ); $product->setStockData ( array ('is_in_stock' => 1 ) ); $product->setCreatedAt ( strtotime ( 'now' ) ); $product->save (); } $instanceParent = $product->getTypeInstance (); /* @var $instanceParent G_Catalog_Model_Product_Type_Configurable */ die ( get_class ( $instanceParent ) ); }
此时创建的子商品还是个超级简单的商品,但是我们的业务目的已经实现了。我还要给子商品设置一些属性,例如颜色、尺码、自定义颜色等。
未完,待续~~~~~~~~~~~~~~

