本api是根据magento 自有api product.info改写而来,大家可以自己根据项目需求改写api返回内容。
至于api配置部分就不再重复,想看配置custom api请点击:magento custom api configuration
/**
* Retrieve product info
*
* @param int|string $productId
* @param string|int $store
* @param array $attributes
* @param string $identifierType
* @return array
*/
public function infomobile($productId, $store = null, $attributes = null, $identifierType = 'sku')
{
$baseUrl = Mage::getBaseUrl();
$baseUrl = str_replace('/index.php/', '', $baseUrl);
$ignoredAttributeTypes = array_flip($this->_ignoredAttributeTypes);
if(in_array('media_image',$this->_ignoredAttributeTypes)){
unset($this->_ignoredAttributeTypes[$ignoredAttributeTypes['media_image']]);
}
if(in_array('gallery',$this->_ignoredAttributeTypes)){
unset($this->_ignoredAttributeTypes[$ignoredAttributeTypes['gallery']]);
}
$product = $this->_getProduct($productId, $store, $identifierType);
/* Mage_Catalog_Helper_Image */
$productImage = Mage::helper('catalog/image');
$mobileImagesUrl = (string)$productImage->init($product, 'image')->resize(640,640);
$mobileImagesUrl = str_replace($baseUrl, '', $mobileImagesUrl);
//$mobileImagesUrl = substr($mobileImagesUrl,strpos($mobileImagesUrl,'/cache/'));
$result = array( // Basic product data
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'categories' => $product->getCategoryIds(),
'websites' => $product->getWebsiteIds()
);
foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
//Mage::log($attribute->getAttributeCode());
if ($this->_isAllowedAttribute($attribute, $attributes)) {
if(in_array($attribute->getAttributeCode(), array('price','special_price','minimal_price','tier_price'))){
$result[$attribute->getAttributeCode()] = (int)$product->getData($attribute->getAttributeCode());
}else if($attribute->getAttributeCode() == 'url_path'){
$result[$attribute->getAttributeCode()] = str_ireplace('index.php/', '', Mage::getBaseUrl()).$product->getData($attribute->getAttributeCode());
}else if(in_array($attribute->getAttributeCode(), array('image','small_image','thumbnail','mobile_image','mobile_thumbnail_image'))){
$result[$attribute->getAttributeCode()] = '/media/catalog/product'.$product->getData($attribute->getAttributeCode());
}else if($attribute->getAttributeCode() == 'media_gallery'){
foreach ($product->getData($attribute->getAttributeCode()) as $images){
$n = 0;
foreach ($images as $image){
foreach ($image as $k=>$v){
if($k == 'file'){
$result[$attribute->getAttributeCode()]['images'][$n]['file'] = $this->_getResizedImage($image['file']);
}else{
$result[$attribute->getAttributeCode()]['images'][$n][$k] = $v;
}
}
$n++;
}
}
}else{
$result[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode());
}
}
}
$result['image_resize640'] = $mobileImagesUrl;
//$categoryIds = $product->getCategoryIds();
$storyId = $product->getFavoriteStoryId();
$result['other_design'] = array();
$collection = array();
$a = 0;
if($storyId) {
$category = Mage::getModel('catalog/category')->load($storyId);
$pcollection = Mage::getResourceModel('catalog/product_collection');
$pcollection->addCategoryFilter($category); //category filter
$pcollection->addAttributeToFilter('status',1); //only enabled product
$pcollection->addAttributeToSelect(array('id','sku','name','url','image','thumbnail','updated_at','size','color','composition','care')); //add product attribute to be fetched
$pcollection->addStoreFilter();
$products =array();
if(!empty($pcollection)){
foreach ($pcollection as $_product){
$collection[$a]['updated_at'] = $_product->getUpdatedAt();
$collection[$a]['id'] = $_product->getId();
$collection[$a]['sku'] = $_product->getSku();
$collection[$a]['name'] = $_product->getName();
$collection[$a]['size'] = $_product->getSize();
$collection[$a]['color'] = $_product->getColor();
$collection[$a]['composition'] = $_product->getComposition();
$collection[$a]['care'] = $_product->getCare();
//$collection[$a]['url'] = $_product->getUrlPath();
$image_resize100 = (string)$productImage->init($_product, 'image')->resize(100,100);
$collection[$a]['image_resize100'] = str_replace($baseUrl, '', $image_resize100);
//$collection[$a]['image_resize100'] = substr($image_resize100,strpos($image_resize100,'/cache/'));
$a++;
}
shuffle($collection);
}
}
$i = 0;
$result['other_design'] = $collection;
$favoriteCategoryId = $product->getFavoriteCategoryId();
$guessYouLike =array();
if(!empty($favoriteCategoryId)){
$favoriteCategoryModel = Mage::getModel('catalog/category')->load($favoriteCategoryId);
$fpcollection = Mage::getResourceModel('catalog/product_collection');
$fpcollection->addCategoryFilter($favoriteCategoryModel); //category filter
$fpcollection->addAttributeToFilter('status',1); //only enabled product
$fpcollection->addAttributeToSelect(array('id','sku','name','url','image','thumbnail','updated_at','size','color','composition','care')); //add product attribute to be fetched
$fpcollection->addStoreFilter();
if(!empty($fpcollection)){
foreach ($fpcollection as $relpdt) {
//Mage::log($relpdt);
$guessYouLike[$i]['updated_at'] = $relpdt->getUpdatedAt();
$guessYouLike[$i]['id'] = $relpdt->getId();
$guessYouLike[$i]['sku'] = $relpdt->getSku();
$guessYouLike[$i]['name'] = $relpdt->getName();
$guessYouLike[$i]['size'] = $relpdt->getSize();
$guessYouLike[$i]['color'] = $relpdt->getColor();
$guessYouLike[$i]['composition'] = $relpdt->getComposition();
$guessYouLike[$i]['care'] = $relpdt->getCare();
$image_resize100 = (string)$productImage->init($relpdt, 'image')->resize(100,100);
$guessYouLike[$i]['image_resize100'] = str_replace($baseUrl, '', $image_resize100);
//$guessYouLike[$i]['image_resize100'] = substr($image_resize100,strpos($image_resize100,'/cache/'));
$i++;
}
}
shuffle($guessYouLike);
}
$result['guess_you_like'] = $guessYouLike;
$this->_ignoredAttributeTypes[] = 'media_image';
$this->_ignoredAttributeTypes[] = 'gallery';
return $result;
}
希望能给刚刚开始接触magento的朋友一点帮助,同时欢迎大家指正我代码的错误之处,非常感谢!