
今天参考网上的几篇文章,终于创建magento的configurable product成功了。magento的Api本身就不太强大,并且其说明文档也不丰富,很多资料都是很多个博客的文章综合到一起才能明白是怎么回事。将我的代码贴出来,做下笔记吧。
参考文章地址:http://www.stephenrhoades.com/?p=338
http://blog.omnisubsole.com/2009/07/01/configurable-products-in-magento/
在使用Magento Api前,首先去给我们的核心Api文件添加一段补充代码(当然不应该直接修改核心文件)。
文件路径:Mage/Catalog/Model/Product/Api.php 的_prepareDataForSave中添加如下代码
/* * Check for configurable products array passed through API Call */ if(isset($productData['configurable_products_data']) && is_array($productData['configurable_products_data'])) { $product->setConfigurableProductsData($productData['configurable_products_data']); } if(isset($productData['configurable_attributes_data']) && is_array($productData['configurable_attributes_data'])) { foreach($productData['configurable_attributes_data'] as $key => $data) { //Check to see if these values exist, otherwise try and populate from existing values $data['label'] = (!empty($data['label'])) ? $data['label'] : $product->getResource()->getAttribute($data['attribute_code'])->getStoreLabel(); $data['frontend_label'] = (!empty($data['frontend_label'])) ? $data['frontend_label'] : $product->getResource()->getAttribute($data['attribute_code'])->getFrontendLabel(); $productData['configurable_attributes_data'][$key] = $data; } $product->setConfigurableAttributesData($productData['configurable_attributes_data']); $product->setCanSaveConfigurableAttributes(1); }
然后是我们的主代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>from www.hellokeykey.com</title> </head> <body> <?php function doProductMigration($productType, $setId, $sku, $productData) { $result = array(); $client = new SoapClient('http://www....magento/api/soap/?wsdl'); $session = $client->login('user', 'apikey'); $client->call($session, 'product.create', array($productType, $setId, $sku, $productData)); $result = $client->call($session, 'product.info', $sku); return $result; $client->endSession($session); } $newProductData = array( 'name' => 'Product Name 1', 'lee_size' => '125', 'websites' => array(1), 'description' => '', 'price' => 9.99, 'category_ids' => '', 'visibility' => 4, 'status' => 1, 'weight' => 1, 'tax_class_id' => 2, ); $sku_c = 'cc' . rand(); $productOne = doProductMigration('simple', '63', $sku_c, $newProductData); echo '<p>productOne strat------------------------</p>'; print_r($productOne); echo '<p>productOne end</p>'; $newProductData = array( 'name' => 'Product Name 2', 'lee_size' => '126', 'websites' => array(1), 'description' => '', 'price' => 9.99, 'category_ids' => '', 'visibility' => 4, 'status' => 1, 'weight' => 1, 'tax_class_id' => 2, ); $sku_c = 'cc' . rand(); $productTwo = doProductMigration('simple', '63', $sku_c, $newProductData); echo '<p>productTwo strat-----from-www.hellokeykey.com----------</p>'; print_r($productTwo); echo '<p>productTwo end</p>'; $configurableProductsData = array( $productOne['product_id'] => array( 'attribute_id' => 950, //[44]=> array(5) { ["attribute_id"]=> string(3) "502" ["code"]=> string(9) "shoe_size" ["type"]=> string(6) "select" ["required"]=> string(1) "1" ["scope"]=> string(6) "global" } 'label' => 'option 1', //[1]=> array(2) { ["value"]=> string(2) "46" ["label"]=> string(1) "3" } 'value_index' => 125, //The option id 'is_percent' => 0, 'pricing_value' => '' ), $productTwo['product_id'] => array( 'attribute_id' => 950, //The attribute id 'label' => 'option 2', 'value_index' => 126, //The option id 'is_percent' => 0, 'pricing_value' => '' ) ); // /Create the configurable attributes data $configurableAttributesData = array( '0' => array( 'id' => NULL, 'label' => '', //optional, will be replaced by the modified api.php 'position' => NULL, 'values' => array( 0 => array( // 'attribute_id' => 950, //The attribute id // 'label' => 'lee size', // 'value_index' => 125, //The option id 'is_percent' => 0, 'pricing_value' => '' ), 1 => array( // 'attribute_id' => 950, //The attribute id // 'label' => 'option 2', // 'value_index' => 126, //The option id 'is_percent' => 0, 'pricing_value' => '' ) ), 'attribute_id' => 950, //get this value from attributes api call 'attribute_code' => 'lee_size', //get this value from attributes api call 'frontend_label' => 'lee size', //optional, will be replaced by the modifed api.php 'html_id' => 'config_super_product__attribute_0' ) ); //add configurable product $newProductData = array( 'name' => 'Product Group Name', 'websites' => array(1), 'description' => 'Group description', 'category_ids' => array(1), 'visibility' => 4, 'status' => 1, 'weight' => 0, 'price' => 9.99, 'tax_class_id' => 2, 'weight' => 0, 'configurable_products_data' => $configurableProductsData, 'configurable_attributes_data' => $configurableAttributesData ); $sku_c = 'cc'.rand(); $result = doProductMigration('configurable', '63', $sku_c, $newProductData); ?> </body> </html>
弄完了看一下,一个是要首先利用Api去获得我们的属性组合属性的值,然后才能将必要参数设置好。我们的Magento Api尽管不是很完善,但是功能加加减减还是不麻烦的。本文章是我个人的测试,仅供大家参考,如有不理解,去看下上面的那个英文地址的文章,或者继续google。
转载表明出处:www.hellokeykey.com
/* * Check for configurable products array passed through API Call */ if(isset($productData['configurable_products_data']) && is_array($productData['configurable_products_data'])) { $product->setConfigurableProductsData($productData['configurable_products_data']); } if(isset($productData['configurable_attributes_data']) && is_array($productData['configurable_attributes_data'])) { foreach($productData['configurable_attributes_data'] as $key => $data) { //Check to see if these values exist, otherwise try and populate from existing values $data['label'] = (!empty($data['label'])) ? $data['label'] : $product->getResource()->getAttribute($data['attribute_code'])->getStoreLabel(); $data['frontend_label'] = (!empty($data['frontend_label'])) ? $data['frontend_label'] : $product->getResource()->getAttribute($data['attribute_code'])->getFrontendLabel(); $productData['configurable_attributes_data'][$key] = $data; } $product->setConfigurableAttributesData($productData['configurable_attributes_data']); $product->setCanSaveConfigurableAttributes(1); }
钥匙,我现在想做一个OPTION里面包含XS,S,M,L,XL,CUSTOM MADE的选项,然后如果客人选了CUSTOM MADE的话,能不能直接出现一个多尺寸输入框表单(不是AJAX),MAGENTO本身能做到么?谢谢~
本身不带这个功能的,还真要通过ajax或者js来实现。
你好 钥匙
我用代码生成了一个configurable product,然后生成了几个simple product,那么如何用代码将他们关联起来呢?这几个simple是属于不同尺寸的产品,都属于这个configurable?
还有您的这篇文章中的$client = new SoapClient(‘http://www….magento/api/soap/?wsdl’);
其中的url是正确的吗?
期待您的解答!
你好,感谢你的关注。这个代码最关键是代码中的属性attribute_id,这个是在magento后台新建属性以后才有的值,可能每一个magento网站都不一样,你要先搞清楚,你要添加的这个可配置商品的这个下拉菜单属性值是多少。本代码中已经用代码实现了如何关联。所以,你仔细看下就会理解了。所有的magento开发都是基于多magento后台的了解,如果你后台不熟,也先看下如何添加一个config product。
很强大!
钥匙哥你好,我参考了你那可配置产品的视频,由于现在版本都发展到1.8了,可能有所出入.
在可配置的产品里面快速创建产品,前台可以实现.
可如果我颜色列表多,尺寸多了,要创建的就是颜色*尺寸的商品数量
这方面有没完善的办法?
貌似是没有好办法的,可以加我QQ咨询
钥匙你好
想问一下你有没有版本更新的生成配置产品php实例啊,现在你这个php demo没办法在1.9版本里创建configurable productk啊
现在不好给你找例子,你google下
这两个属性原生api应该没有吧,configurable_products_data,configurable_attributes_data