复制 \app\code\core\Mage\Reports\Model\Resource\Quote\Collection.php 并粘贴到 app\code\local\Mage\Reports\Model\Resource\Quote\Collection.php 中
重写 公共函数 prepareForProductsInCarts() 函数
public function prepareForProductsInCarts()
{
$productEntity = Mage::getResourceSingleton('catalog/product_collection');
$productAttrName = $productEntity->getAttribute('name');
$productAttrNameId = (int) $productAttrName->getAttributeId();
$productAttrNameTable = $productAttrName->getBackend()->getTable();
$productAttrPrice = $productEntity->getAttribute('price');
$productAttrPriceId = (int) $productAttrPrice->getAttributeId();
$productAttrPriceTable = $productAttrPrice->getBackend()->getTable();
$ordersSubSelect = clone $this->getSelect();
$ordersSubSelect->reset()
->from(
array('oi' => $this->getTable('sales/order_item')),
array(
'orders' => new Zend_Db_Expr('COUNT(1)'),
'product_id'))
->group('oi.product_id');
$this->getSelect()
->useStraightJoin(true)
->reset(Zend_Db_Select::COLUMNS)
->joinInner(
array('quote_items' => $this->getTable('sales/quote_item')),
'quote_items.quote_id = main_table.entity_id',
null)
->joinInner(
array('e' => $this->getTable('catalog/product')),
'e.entity_id = quote_items.product_id',
null)
->joinInner(
array('product_name' => $productAttrNameTable),
"product_name.entity_id = e.entity_id AND product_name.attribute_id = {$productAttrNameId}",
array('name'=>'product_name.value'))
->joinInner(
array('product_price' => $productAttrPriceTable),
"product_price.entity_id = e.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
array('price' => new Zend_Db_Expr('product_price.value * main_table.base_to_global_rate')))
->joinLeft(
array('order_items' => new Zend_Db_Expr(sprintf('(%s)', $ordersSubSelect))),
'order_items.product_id = e.entity_id',
array()
)
->columns('e.*')
->columns(array('carts' => new Zend_Db_Expr('COUNT(quote_items.item_id)')))
->columns('order_items.orders')
->where('main_table.is_active = ?', 1)
->group('quote_items.product_id');
return $this;
}
并用下面的函数替换。
public function prepareForProductsInCarts()
{
$productEntity = Mage::getResourceSingleton('catalog/product_collection');
$productAttrName = $productEntity->getAttribute('name');
$productAttrNameId = (int) $productAttrName->getAttributeId();
$productAttrNameTable = $productAttrName->getBackend()->getTable();
$productAttrPrice = $productEntity->getAttribute('price');
$productAttrPriceId = (int) $productAttrPrice->getAttributeId();
$productAttrPriceTable = $productAttrPrice->getBackend()->getTable();
$ordersSubSelect = clone $this->getSelect();
$ordersSubSelect->reset()
->from(
array('oi' => $this->getTable('sales/order_item')),
array(
'orders' => new Zend_Db_Expr('COUNT(1)'),
'product_id'))
->group('oi.product_id');
$this->getSelect()
->useStraightJoin(true)
->reset(Zend_Db_Select::COLUMNS)
->joinInner(
array('quote_items' => $this->getTable('sales/quote_item')),
'quote_items.quote_id = main_table.entity_id',
null)
->joinInner(
array('e' => $this->getTable('catalog/product')),
'e.entity_id = quote_items.product_id',
null)
->joinInner(
array('product_name' => $productAttrNameTable),
"product_name.entity_id = e.entity_id AND product_name.attribute_id = {$productAttrNameId}",
array('name'=>'product_name.value'))
->joinInner(
array('product_price' => $productAttrPriceTable),
"product_price.entity_id = e.entity_id AND product_price.attribute_id = {$productAttrPriceId}",
array('price' => new Zend_Db_Expr('product_price.value * main_table.base_to_global_rate')))
### Newly added script ###
->joinLeft(
array('cpei' => 'catalog_product_entity_int'),
'cpei.entity_id = e.entity_id AND cpei.attribute_id = 92',
array()
)
->joinLeft(
array('eaov' => 'eav_attribute_option_value'),
'eaov.option_id = cpei.value',
array('color'=>'eaov.value')
)
### End script ###
->joinLeft(
array('order_items' => new Zend_Db_Expr(sprintf('(%s)', $ordersSubSelect))),
'order_items.product_id = e.entity_id',
array()
)
->columns('e.*')
->columns(array('carts' => new Zend_Db_Expr('COUNT(quote_items.item_id)')))
->columns('order_items.orders')
->where('main_table.is_active = ?', 1)
->group('quote_items.product_id');
return $this;
}
这里 cpei.attribute_id = 92 是我的“颜色”属性 id。您可以根据需要进行更改。
也将下面的代码放入
\app\code\core\Mage\Adminhtml\Block\Report\Shopcart\Product\grid.php 文件。
$this->addColumn('color', array(
'header' =>Mage::helper('reports')->__('Color'),
'index' =>'color',
是的,我们可以将 grid.php 文件从核心复制到本地文件夹。
在 magento 1.7 中工作和测试