标签归档:API

Magento 使用Api将Sku添加到Url中

Magento api add sku to url

Magento api add sku to url

使用Magento Api修改商品Url路径的测试程序,希望对大家理解Api有帮助.

主要目的是通过Api去将商品的sku编号加到商品Url的后面,批量来更新Url,代码如下。

<?php
$proxy = new SoapClient('http://www.yourdomain.com/api/soap/?wsdl');
$session = $proxy->login('username', 'abc1234');

//获取全部商品
$products = $proxy->call($session, 'product.list');
//print_r($products);
echo count($products);
$i = 0;
foreach($products as $product)
{
$i++;
if($i>249 && $i<300)
{
echo $product[sku]."-Start<br />";
//获取商品url
$product_one = $proxy->call($session, 'product.info', $product[sku]);
//print_r($proxy->call($session, 'product.info', $product[sku]));
$new_url = $product_one[url_path].'-'.strtolower($product[sku]);
echo $proxy->call($session, 'catalog_product.update', array($product[sku], array('url_path'=> $new_url)));
echo $proxy->call($session, 'catalog_product.update', array($product[sku], array('url_key'=> $new_url)));

//print_r($proxy->call($session, 'product.info', $product[sku]));
//echo $new_url;
echo $product[sku]."-OK-".$i."<br />";
}
//break;
}

//获取商品sku
//$proxy->call($session, 'product.info', 'sku');

转载标明出处:www.hellokeykey.com

使用Magento的api更新tier price

magento tier price

magento tier price

写了段更新Magento tier price的代码,供初学者参考。此代码主要是用来改变Magento tier price的用户组。代码如下

//初始化Api连接
$proxy = new SoapClient('http://www.网址.com/api/soap/?wsdl');
$sessionId = $proxy->login('key', '123456');
 function changetierprices($sku,$proxy,$sessionId)
{
	// Get tier prices
	//$sku ='JScd122';
	$tierPrices = $proxy->call($sessionId, 'product_tier_price.info', $sku);

	//判断是否有tier prices
		 if(count($tierPrices))
		{
			foreach ($tierPrices as $key1=>$value1)
			{
				if($value1[customer_group_id] == '1') 
				{
					//修改tier price的组 
					$tierPrices[$key1]['customer_group_id'] = 'all';

				}

			}
			//update
			$proxy->call($sessionId, 'product_tier_price.update', array($sku, $tierPrices));
			//echo '<p>update</p>';
		}
		else
		{
			echo '<p>this one no tierprice,sku='.$sku.'</p>'; 
		} 

}

继续阅读

获得Magento 1.5 全部Api方法

获取Magento全部Api方法列表

获取Magento全部Api方法列表

Magento官方的Api文档更新不是很快,有些Magento Api方法可能没有列出来。下面给出获取Magento全部Api方法的代码。其中的Api连接信息,换成你自己的。

此文基于magento1.5版本,其余版本可能稍有不同。

Magento官方文档见:Magento core Api

<!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>获得全部的Magento Api方法 www.hellokeykey.com</title>
<style type="text/css">
dl{}
dl dt{}
dl dd{ }
.box1{ border:2px solid #000; margin-bottom:10px; padding:10px;}
.box1 .path{ margin-bottom:10px; border-bottom-style:dashed; border-bottom-color:#000000; border-bottom-width:1px;}
</style>
</head>

<body>

	<?php
	$client = new SoapClient('http://www......./api/soap/?wsdl');
    $session = $client->login('ApiUser', 'ApiKey');
	$result = $client->resources($session);
	foreach($result as $k1=>$v1)
	{
		echo '<div class="box1"><dl>';
		echo '<dt>Title:</dt><dd>'.$v1[title].'</dd>';
		echo '<dt>Name:</dt><dd>'.$v1[name].'</dd>';
		echo '<dt>methods:</dt><dd>';
		if(count($v1[methods])){
			echo '<ul>';
			foreach($v1[methods] as $k2=>$v2)
			{
				echo '<li>Title:'.$v2[title].'</li>';
				echo '<li>Name:'.$v2[name].'</li>';
				echo '<li class="path">Path:'.$v2[path].'</li>';
			}
			echo '</ul></dd>';
		}
		echo '</dl></div>';
	}
	?>
</body>
</html>

转载表明出处:www.hellokeykey.com

 

使用Magento Api给产品添加图片

magento api update product images

magento api update product images

magento的Api默认就有更新产品图片的功能,今天将我自己测试的代码共享下,供大家学习参考。本次使用magento 1.5版Api,其它版本可能稍有不同。

代码片段如下:

//省略初始化Api过程,见我的其余magento API文章 www.hellokeykey.com
//        产品图片
//        初始化产品图片信息,注意自己上传个产品图片到magento的产品图片文件夹
//        $imagePath为图片路径,如果你使用相对路径,注意测试下是正确
//        label为图片的alt属性
//        position 为图片的显示顺序
//        type 为此图片作为'thumbnail','small_image','image'中的哪一个
//        mime为图片类型
        $imagePath = "http://www.......magento/media/catalog/product/h/t/htc-touch-diamond.jpg";  //产品图片路径
        $newImage = array(
            'file' => array(
                'name' => 'file_name',
                'content' => base64_encode(file_get_contents($imagePath)),
                'mime' => 'image/jpeg'
            ),
            'label' => 'Cool Image Through Soap',
            'position' => 1,
            'types' => array('thumbnail', 'small_image', 'image'),
            'exclude' => 0
        );
//使用 Api创建产品图片
$imageFilename = $client->call($session, 'product_media.create', array($sku, $newImage))

继续阅读

使用Magento Api创建configurable product

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。

继续阅读