magento app开发遇到的问题及解决
今天一直在解决Magento的APP接口调用数据异常的问题,调用/api/rest/category/:id 这个接口的时候,返回的所有目录的数据是一样的,原始代码是这样的.
1)请求地址 /api/rest/category/3
2) 请求地址请求的是对应的类的_retrieve这个方法
protected function _retrieve()
{
$category_id = $this->getRequest()->getParam('id');
$categorys = array();
$nodes =Mage::getModel('catalog/category')->getCategories($category_id);
$nodes_count = count($nodes);
foreach($nodes as $node)
{
$category = Mage::getModel('catalog/category')->load($node->getId());
$data = $this->_renderData($category,$category_id,$nodes_count);
if($data) $categorys[] = $data;
}
return $categorys;
}
3)_retrieve这个方法调用的_renderData这个方法(没修改之前)
protected function _renderData($category, $category_id,$nodes_count)
{
$item = array();
$item['id'] = $category->getId();
$item['name'] = $category->getName(); //skip brands
if($category->getUrlKey()=='brands') return @$data; $item['url_key'] = $category->getUrlKey();
$item['thumbnail'] = $this->getThumbnailUrl($category);
$item['child'] = array();
$category_id = $this->getRequest()->getParam('id');
$category = Mage::getModel('catalog/category')->load($category_id);
$ids = $category->getChildren();
$id_array = explode(',', $ids);
$product_array = array();
foreach ($id_array as $key => $value) {
$products = Mage::getModel('catalog/category')->load($value)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4); $product_data =array();
foreach ($products as $product ) {
$image = $product->getImage();
$image_url = $this->getProductImageUrl($image);
$product_data[]= array(
'name'=>$product->getName(),
'sku' =>$product->getSku(),
'image'=>$image_url,
'price'=>$product->getPrice(),
'weight'=>$product->getWeight(),
'short_description'=>$product->getShort_description()
);
}
$product_array[] = $product_data;
}
$item['child']= $product_array; retrun $item;
}
运行了这些代码后,所有子目录下面都是一样的产品!!! 其实问题很简单,不用foreach两层嵌套这么麻烦,其实每次在_retrieve这个方法执行的时候其实就是给单独的产品追加属于自己的产品就可以了,如果像第三步一样,就相当于给所有目录就加上了一样的产品,_renderData改成下面的代码即可
4)正确代码
protected function _renderData($category, $category_id,$nodes_count)
{
$item = array();
$item['id'] = $category->getId();
$item['name'] = $category->getName(); //skip brands
if($category->getUrlKey()=='brands') return @$data; $item['url_key'] = $category->getUrlKey();
$item['thumbnail'] = $this->getThumbnailUrl($category);
$item['child'] = array(); $products = Mage::getModel('catalog/category')->load($item['id'] )
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4); $product_data =array();
foreach ($products as $product ) {
$image = $product->getImage();
$image_url = $this->getProductImageUrl($image);
$product_data[]= array(
'name'=>$product->getName(),
'sku' =>$product->getSku(),
'image'=>$image_url,
'price'=>$product->getPrice(),
'weight'=>$product->getWeight(),
'short_description'=>$product->getShort_description()
);
}
$item['child'] = $product_data;
return $item;
}
改成上面的代码后,就正常了,工作中一定要弄懂基础的原理,问题有时候就迎刃
magento app开发遇到的问题及解决的更多相关文章
- C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。
原文:C# Windows Phone App 开发,修改[锁定画面],从[Assets].[UI].[网路图片],并解决失灵问题. 一般我们在开发Windows Phone App,有时会希望透过应 ...
- android软件简约记账app开发day05-记账页面条目代码优化和bug解决
android软件简约记账app开发day05-记账页面条目代码优化和bug解决 今天还是因为该bug又极大的耽误了项目进程,该开发文档都要没有时间来写了. 先说bug吧,在昨天已经实现了页面图标的展 ...
- 从中间件的历史来看移动App开发的未来
在移动开发领域我们发现一个很奇怪的现象:普通菜鸟新手经过3个月的培训就可以拿到 8K 甚至上万的工作:在北京稍微有点工作经验的 iOS 开发,就要求 2 万一个月的工资.不知道大家是否想过:移动应用开 ...
- App开发的新趋势
移动开发这些年,移动开发者人数越来越多,类似的培训公司发展也很快,不过伴随着的是移动应用的需求这几年发展更为旺盛.要开发好的App,纯原生开发肯定是最佳选择.但是这么多年发展,原生开发的难度并没有降低 ...
- html5 app开发,你知道多少?
随着大量新生移动设备的兴起,改变了互联网的未来,html5技术为开发者提供了一个跨平台的移动app开发方案,并且该方案具有很好的扩展性和灵活性,但就国内使用html5开发app应用技术来讲,还是有欠缺 ...
- 选择App开发外包时,你该了解哪些法律常识?
随着App需求的激增,选择App外包服务的客户也多了起来.然而客户和开发方对于其中的法律条款却不甚了解,导致在服务过程中,时常会发生一些分歧和纠纷,最终致使项目搁浅. 为了普及App外包的法律常识,移 ...
- phongap、APICloud、ionic等app开发平台你都知道吗?
大众创业热,很多人都想在互联网大展拳脚,然而大部分人却是非技术背景.针对这个行业痛点,现在国内外涌现出众多APP开发工具,开发者只要有相关的HTML5.CSS和JavaScript知识,便可以轻松快速 ...
- 手机app开发:浅谈APP登录方式的优劣
手机app开发公司亿合科技要是给你一个机会设计一款APP,你会用什么方式做这个APP的登录模块?根据APP的业务模型的不同会有不同的设计方法.如果是偏内容型的APP,需要优先展示内容给用户,当用户需要 ...
- Cordova+ionic 开发hybird App --- 开发环境搭建
Cordova 开发hybird App 开发环境搭建 一.一些基础概念: Ant : 简单说来可以这么理解,如果你用记事本写JAVA程序,然后在cmd里输入javac命令编译它,但是有一天你发现每次 ...
随机推荐
- GO语言练习:网络编程 TCP 示例
1.代码 2.编译及运行 1.网络编程 TCP 示例 simplehttp.go 代码 package main import ( "net" "os" &qu ...
- 符号三角形——F
F. 符号三角形 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: Java class name: 符号 ...
- 抄书 Copying Books UVa 714
Copying Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...
- [CareerCup] 16.3 Dining Philosophers 哲学家聚餐问题
16.3 In the famous dining philosophers problem, a bunch of philosophers are sitting around a circula ...
- 使用explain查看mysql查询执行计划
explain语句: 字段解释: type: all(全表扫描) ref() possible_keys: 预测使用什么列做为索引 key: 实际使用的key ...
- 面向对象static静态的属性和方法的调用
<?php header("content-type:text/html;charset=utf-8"); class Human{ static public $name ...
- gvim配置
colorscheme darkblue set lines=100 set columns=150
- 【转】java开源类库pinyin4j的使用
最近CMS系统为了增加查询的匹配率,需要增加拼音检索字段,在网上找到了pinyin4j的java开源类库,提供中文转汉语拼音(并且支持多音字), 呵呵,看了看他的demo,决定就用它了,因为我在实际使 ...
- rbd命令
chen@admin-node:~$ rbd --help usage: rbd [-n <auth user>] [OPTIONS] <cmd> ... where 'poo ...
- Different Approaches for MVCC
https://www.enterprisedb.com/well-known-databases-use-different-approaches-mvcc Well-known Databases ...