1.在后台中新建产品属性salestotal用作销量排序
2.新建Tang/ProductSales模块
3.配置模块的config.xml文件
<?xml version="1.0"?>
<config>
<modules>
<Tang_ProductSales>
<version>0.1.0</version>
</Tang_ProductSales>
</modules>
<frontend>
<routers>
<productsales>
<use>standard</use>
<args>
<module>Tang_ProductSales</module>
<frontName>productsales</frontName>
</args>
</productsales>
</routers>
<layout>
<updates>
<productsales>
<file>productsales.xml</file>
</productsales>
</updates>
</layout>
</frontend>
<global>
<models>
<productsales>
<class>Tang_ProductSales_Model</class>
<resourceModel>productsales_resource</resourceModel>
</productsales>
<!-- st 数据库资源模型配置 -->
<productsales_resource>
<class>Tang_ProductSales_Model_Resource</class>
<deprecatedNode>productsales_mysql4</deprecatedNode>
<entities>
<catalog_product_entity_varchar>
<table>catalog_product_entity_varchar</table>
</catalog_product_entity_varchar>
</entities>
</productsales_resource>
<!-- end 数据库资源模型配置 -->
</models>
<events>
<checkout_onepage_controller_success_action>
<observers>
<productsales>
<!--
此处可以model、object、singleton
1. model和object是等效的且它们都将使用Mage::getModel(...)方法来进行初始化
2. 而singleton将使用Mage::getSingleton(...)来初始化
-->
<type>model</type>
<class>productsales/observer</class>
<method>preOrderSubmitInsertProductSales</method>
</productsales>
</observers>
</checkout_onepage_controller_success_action>
</events>
<helpers>
<productsales>
<class>Tang_ProductSales_Helper</class>
</productsales>
</helpers>
<blocks>
<productsales>
<class>Tang_ProductSales_Block</class>
</productsales>
</blocks>
<resources>
<productsales_write>
<connection>
<use>core_write</use>
</connection>
</productsales_write>
<productsales_read>
<connection>
<use>core_read</use>
</connection>
</productsales_read>
</resources>
</global>
</config>
4.配置监听时间,当提交订单后,订单中的产品销量属性加一
<?php
class Tang_ProductSales_Model_Observer extends Mage_Catalog_Model_Observer
{
public function preOrderSubmitInsertProductSales(Varien_Event_Observer $observer){
$order = new Mage_Sales_Model_Order();
$incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId(); //订单ID
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId); //订单信息
$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item) {
//获取指定产品对象
$prcProject = Mage::getModel('catalog/product')->load($item->getProductId());
$attrProject = Mage::getModel('eav/entity_attribute')->load('renqi','attribute_code'); //获取指定属性标识码的属性信息
$entypeid = $attrProject->getEntity_type_id(); //Eav类型
$attrid = $attrProject->getAttribute_id(); //属性ID
$storeid = 0; //商店ID
$entityid = $item->getProductId(); //产品ID
$_product = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('sku', $prcProject->getSku())
->setOrder('ordered_qty', 'desc')
->getFirstItem();
$svalue = (int)$_product->getOrderedQty();
// $svalue = (int)$_product->addOrderedQty() + $prcProject->getSalestotal(); //销量累加
if($svalue >0){
$ifused = true;
}else{
$ifused = false;
}
Mage::getResourceSingleton('productsales/salestotal')->updProductSalesTotal($entypeid, $attrid, $storeid, $entityid, $svalue, $ifused);
}
return $this;
}
}
4.对销量属性进行写入:
<?php
class Tang_ProductSales_Model_Resource_Salestotal extends Mage_Core_Model_Resource_Db_Abstract
{
protected function _construct() {
$this->_init("productsales/catalog_product_entity_varchar","value_id");
}
/**
* 添加产品销量到产品信息中
* @param int $entypeid eav类型
* @param int $attrid 属性ID
* @param int $storeid 商店ID
* @param int $entityid 产品ID
* @param varchar $svalue 销量
* @param bool $ifused 是否已存在
* @return number
*/
public function updProductSalesTotal($entypeid, $attrid, $storeid, $entityid, $svalue, $ifused){
$selfread = $this->_getConnection('productsales_write');
$table = $this->getTable('productsales/catalog_product_entity_varchar');
//存入数组的数据
$sdata = array("entity_type_id"=>$entypeid, 'attribute_id'=>$attrid, 'store_id'=>$storeid, 'entity_id'=>$entityid, 'value'=>$svalue);
//查询条件
if($ifused){
$swhere = " attribute_id=$attrid"." AND entity_id=$entityid";
//执行更新操作
return $selfread->update($table, $sdata, $swhere);
}else{
//执行插入操作
return $selfread->insert($table, $sdata);
}
}
}
具体可以参考旺铺商城的项目
网友评论