magento custom category api (2) --- collectioninfo

获取指定分类信息,及所有改分类下的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;
    }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值