获取指定分类信息,及所有改分类下的product信息
查看custom api 配置请点击:magento custom api configuration
/* Retrieve category collection info
*
* @param int $categoryId
* @param string|int $store
* @param array $attributes
* @return array
*/
public function collectioninfo($categoryId, $store = null, $attributes = null)
{
$mediaDir = "/media/catalog/category/";
$baseUrl = Mage::getBaseUrl();
$baseUrl = str_replace('/index.php/', '', $baseUrl);
$category = $this->_initCategory($categoryId, $store);
//error_log("\n----\$collectioninfo ------\n".var_export($category->getData(),true),3,Mage::getBaseDir().'/var/log/categoryCollectioninfo.log');
// Basic category data
$result = array();
$result['category_id'] = $category->getId();
$result['is_active'] = $category->getIsActive();
$result['position'] = $category->getPosition();
$result['level'] = $category->getLevel();
foreach ($category->getAttributes() as $attribute) {
if ($this->_isAllowedAttribute($attribute, $attributes)) {
if($attribute->getAttributeCode() == 'description'){
$result[$attribute->getAttributeCode()] = strip_tags($category->getData($attribute->getAttributeCode()));
}else if(in_array($attribute->getAttributeCode() ,array('image','thumbnail'))){
$result[$attribute->getAttributeCode()] = $category->getData($attribute->getAttributeCode()) ? $mediaDir.$category->getData($attribute->getAttributeCode()) :'';
}else{
$result[$attribute->getAttributeCode()] = $category->getData($attribute->getAttributeCode());
}
}
}
$result['parent_id'] = $category->getParentId();
$result['children'] = $category->getChildren();
$result['all_children'] = $category->getAllChildren();
$result['mobile_thumbnail_image'] = $category->getMobileThumbnailImage() ? $mediaDir.$category->getMobileThumbnailImage():'';
$result['mobile_background_image'] = $category->getMobileBackgroundImage() ? $mediaDir.$category->getMobileBackgroundImage() : '';
$result['mobile_background_image_blur'] = $category->getMobileBackgroundImageBlur() ? $mediaDir.$category->getMobileBackgroundImageBlur() : '';
$pcollection = Mage::getResourceModel('catalog/product_collection');
$pcollection->addCategoryFilter($category); //category filter
$pcollection->addAttributeToFilter('status',1); //only enabled product
$pcollection->addAttributeToSelect(array('id','sku','price','special_price','image','small_image','thumbnail','updated_at')); //add product attribute to be fetched
$pcollection->addStoreFilter();
/* Mage_Catalog_Helper_Image */
$productImage = Mage::helper('catalog/image');
if(!empty($pcollection)){
$a= 0;
foreach ($pcollection as $_product){
//error_log("\n----\$collectioninfo \$_product------\n".var_export($_product->getData(),true),3,Mage::getBaseDir().'/var/log/categoryCollectioninfo.log');
$collection[$a]['id'] = $_product->getId();
$collection[$a]['sku'] = $_product->getSku();
$collection[$a]['special_price'] = (int)$_product->getSpecialPrice();
$collection[$a]['price'] = (int)$_product->getPrice();
$collection[$a]['thumbnail'] = $_product->getThumbnail() ? "/media/catalog/product/".$_product->getThumbnail() :'';
$collection[$a]['image_resize396'] = str_replace($baseUrl, '', (string)$productImage->init($_product, 'image')->resize(396,396));
$collection[$a]['image_resize192'] = str_replace($baseUrl, '', (string)$productImage->init($_product, 'image')->resize(192,192));
$collection[$a]['updated_at'] = $_product->getUpdatedAt();
// $image_resize396 = (string)$productImage->init($_product, 'image')->resize(396,396);
// $image_resize192 = (string)$productImage->init($_product, 'image')->resize(192,192);
// $collection[$a]['image_resize396'] = substr($image_resize396,strpos($image_resize396,'/cache/'));
// $collection[$a]['image_resize192'] = substr($image_resize192,strpos($image_resize192,'/cache/'));
$a++;
}
}
$result['products'] = $collection;
return $result;
}