magento custom product api (1)--- infomobile

11 篇文章 0 订阅
7 篇文章 0 订阅

本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的朋友一点帮助,同时欢迎大家指正我代码的错误之处,非常感谢!






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值