使用Magento Api创建configurable product

piaoling  2011-05-02 11:49:27
Magento Api create configurable product

Magento Api create configurable product

今天参考网上的几篇文章,终于创建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

 

类别 :  magento(258)  |  浏览(3396)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: