zoukankan      html  css  js  c++  java
  • magento根据分类中商品的总浏览次数为分类排行

    今天再次搞magento,完成了一个根据分类中所有商品的总浏览次数对分类排行的功能。

    总流程如下:

    获取网站的所有事件,获取商品的浏览事件,根据商品所属分类(category)对事件进行分组,对各分类(category)的浏览次数进行排序,生成二维数组供前台调用,前台显示。

    核心代码如下:

    1、动态获取数据库连接

    $host = (String) Mage::getConfig()->getNode('global/resources/default_setup/connection/host');//获取主机名
    $dbname = (String) Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname'); //获取数据库名
    $user = (String) Mage::getConfig()->getNode('global/resources/default_setup/connection/username');//获取数据库管理员名
    $pwd = (String) Mage::getConfig()->getNode('global/resources/default_setup/connection/password');//获取数据库密码

    $conn=@mysql_connect($host,$user,$pwd) or die ("error");//连接数据库
    mysql_select_db($dbname,$conn);//选择数据库(makingware)
    mysql_query("set names 'utf8'");//设置查询编码

    2、查询数据库

    $sql='select a.entity_id as id,sum(c.event_id) as num from';//查询分类id  和该分类中商品的总浏览次数
    $sql=$sql.' catalog_category_entity a,catalog_product_entity b,report_event c,report_event_types d ,catalog_category_product e ';//所有涉及的数据库
    $sql=$sql.'where c.event_type_id=d.event_type_id and d.event_name="catalog_product_view" ';//选择事件为“商品浏览事件”的事件记录(事件记录有浏览商品、下单、推荐给好友等)
    $sql=$sql.'and b.entity_id=c.object_id and b.entity_type_id=4 and b.entity_id=e.product_id and e.category_id=a.entity_id and a.parent_id=1';//查询条件:查询网站事件记录表中有记录的商品,且商品类型为4(其实就是商品),再查商品所属的分类,且分类是根分类的子分类(不包括根分类)

    $sql=$sql.'and b.entity_id=c.object_id and b.entity_type_id=4 and b.entity_id=e.product_id and e.category_id=a.entity_id';////查询条件:查询网站事件记录表中有记录的商品,且商品类型为4(其实就是商品),再查商品所属的分类,且分类是根分类的子分类(包括根分类)
    $sql=$sql.' group by a.entity_id order by sum(c.event_id) desc';//根据分类来分组,并根据总浏览次数降序排序

    3、生成二维数组

    $cate=array();
    $result=mysql_query($sql);
    if($result){
    while($row = mysql_fetch_array($result))
    {
    $id=$row["id"];
    $num=$row["num"];
    $cate[]=array("id"=>$id,"num"=>$num);
    }
    }

    $this->setCategory($cate);//应该提前声明一个内部变量

    4、前台显示

    <?php if(($_products = $this->getCategory()) && count($_products)): ?>

    <div class="category-products">
    <?php foreach ($_products as $_product): ?>
    <?php $cat = Mage::getModel('catalog/category')->load($_product["id"]); ?> //因为查询的是分类id,所以要load一下
    <div class="products-grid">
    <?php if($cat->getIsActive()): ?>
    <dl class="item" style="712px;height:294px;">
    <dd style="border:2px solid #e8a742;704px;height:294px;">
    <?php $catUrl=Mage::getBaseUrl('media').'catalog'.DS.'category'.DS.$cat->getThumbnail();?>
    <a href="<?php echo $cat->getUrl() ?>" class="product-image">
    <img src="<?php echo $catUrl; ?>" width="700" height="290" alt="<?php echo $this->htmlEscape($this->getImageLabel($cat, 'small_image')) ?>" />//显示分类的缩略图
    </a>
    </dd>
    </dl>
    <?php endif?>
    </div>
    <?php endforeach ?>
    <script type="text/javascript">decorateDataList('div.products-grid')</script>
    </div>

    <?php endif;?>

  • 相关阅读:
    【Java集合】JDK1.7和1.8 HashMap有什么区别
    【Java集合】为什么HashMap的长度是2的N次幂?
    【VritualEnv】虚拟环境的介绍和基本使用
    【分布式事务】分布式事务解决方案
    【JVM】JVM中的垃圾收集器
    jQuery事件触发前后进行其他的操作
    在jQuery中使用自定义属性
    使用其他服务器引入JS文件
    引入其他服务的JS、和 本地的JS文件,script的属性
    trigger 和 triggerHandler(),自定义事件
  • 原文地址:https://www.cnblogs.com/xiaoSoldier/p/2581432.html
Copyright © 2011-2022 走看看