想添加一个这样的模块 然后调用指定分类下面的产品。
本文由 happysunzone 创作,采用 知识共享署名 3.0 中国大陆许可协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处。
1.fecshop的目前的首页:
计算代码部分:https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/modules/Cms/block/home/Index.php
模板文件部分:https://github.com/fecshop/yii2_fecshop/blob/master/app/appfront/theme/base/front/cms/home/index.php
2.得到某个分类下的产品
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
/** * @property $filter|array * get artile collection by $filter * example filter: * [ * 'numPerPage' => 20, * 'pageNum' => 1, * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ], * 'where' => [ * ['>','price',1], * ['<=','price',10] * ['sku' => 'uk10001'], * ], * 'asArray' => true, * ] * 根据传入的查询条件,得到产品的列表 */ protected function actionColl($filter = '') { return $this->_product->coll($filter); }
这个是在这里实现的:https://github.com/fecshop/yii2_fecshop/blob/master/services/product/ProductMongodb.php
你可以这样写:
$filter = [ 'numPerPage' => 20, 'pageNum' => 1, //'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ], 'where' => [ ['category' => 'xxxxxxxxxxx'] // xxxxxxxx是你的mongodb中的category表 id ], 'asArray' => true, ]; $data = Yii::$service->product->coll($filter); var_dump($data['coll']); var_dump($data['count']);
var_dump($data['coll']); var_dump($data['count']);
那请问大神 获取的上面的数据 如何通过 调用字段 怎么写? 如何显示 产品列表?
<?php if(is_array($products) && !empty($products)): ?> <?php foreach($products as $product): ?>
调用字段 怎么写? 如何显示 产品列表?
<?php endforeach; ?> <?php endif; ?> 下面是我的代码 <?php
$filter = [ 'numPerPage' => 20, 'pageNum' => 1, 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ], 'where' => [ ['sku' => ['p10001-kahaki-xl', 'sk10003-001', 'sk10005', 'sk1000-khak', 'sk0003', 'men0003', 'men0002', 'men0001',]], ], 'asArray' => true, ]; $data = Yii::$service->product->coll($filter); <?php if(is_array($data['coll']) && !empty($data['coll'])): ?> <?php foreach($data['coll'] as $product['coll']): ?> <div class="fl deli-item"> <div><a href="<?= $product['coll']['url'] ?>"> <img class="js_lazy" src="<?= Yii::$service->product->image->getResize($product['image'],[850,430],false) ?>" /> </a> </div> <div class="introduction"> <div class="item-title"><a href="<?= $product['coll']['url'] ?>"><?= $product['coll']['name'] ?></a></div> <div class="item-des">健脑益智,让大脑更聪明</div> <div class="item-price"> <?php $config = [ 'class' => 'fecshop\app\appfront\modules\Catalog\block\category\Price', 'view' => 'catalog/category/price.php', 'price' => $product['price'], 'special_price' => $product['special_price'], 'special_from' => $product['special_from'], 'special_to' => $product['special_to'], ]; echo Yii::$service->page->widget->renderContent('category_product_price',$config); ?> </div> <div class="shopping-cart"><a href="<?= $product['coll']['url'] ?>">了解产品>></a></div> </div> </div>
<?php endforeach; ?> <?php endif; ?>