2013-02-13 34 views
1

被卖我期待加入到产品格列(在管理方面予以明确),以显示有多少次这样的产品已经销往显示号码。以下是我从其他几个职位拼凑在一起后至今:的Magento:次的产品在产品网格

在应用程序/代码/本地/命名空间/ Qtysold /座/ Adminhtml /目录/产品/ Grid.php

<?php 

class Namespace_Qtysold_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid 
{ 
/* Overwritten to be able to add custom columns to the product grid. Normally 
* one would overwrite the function _prepareCollection, but it won't work because 
* you have to call parent::_prepareCollection() first to get the collection. 
* 
* But since parent::_prepareCollection() also finishes the collection, the 
* joins and attributes to select added in the overwritten _prepareCollection() 
* are 'forgotten'. 
* 
* By overwriting setCollection (which is called in parent::_prepareCollection()), 
* we are able to add the join and/or attribute select in a proper way. 
* 
*/ 
public function setCollection($collection) 
{ 
    /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */ 

    $store = $this->_getStore(); 

    if ($store->getId() && !isset($this->_joinAttributes['qty_sold'])) { 
     $collection->joinAttribute(
      'qty_sold', 
      'reports/product_collection', 
      'entity_id', 
      null, 
      'left', 
      $store->getId() 
     ); 
    } 
    else { 
     $collection->addAttributeToSelect('qty_sold'); 
    } 

    echo "<pre>"; 
    var_dump((string) $collection->getSelect()); 
    echo "</pre>"; 

    parent::setCollection($collection); 
} 

protected function _prepareColumns() 
{ 
    $store = $this->_getStore(); 
    $this->addColumnAfter('qty_sold', 
     array(
      'header'=> Mage::helper('catalog')->__('Qty Sold'), 
      'type' => 'number', 
      'index' => 'qty_sold', 
     ), 
     'price' 
    ); 

    return parent::_prepareColumns(); 
} 
} 

一对夫妇这里的东西。 1)$ store->的getId()返回0,所以它永远不会成第一块setCollection,是正确的行为,因为它是管理区域? 2)如果我强迫joinAttribute运行,它会导致它有点意料之中的,因为报告没有出现真正成为一个实体异常(无效的实体...),但我不是在这整个实体企业真正清楚。 3)在其他例子(像这样的:http://www.creativemediagroup.net/creative-media-web-services/magento-blog/30-show-quantity-sold-on-product-page-magento),他们使用的是这样的:

$_productCollection = Mage::getResourceModel('reports/product_collection') 
->addOrderedQty($from, $to, true) 
->addAttributeToFilter('sku', $sku) 
->setOrder('ordered_qty', 'desc') 
->getFirstItem(); 

而且我不知道是否有任何方式“加盟”本报告/ product_collection或是否有什么办法重新创建其“addOrderedQty”数据?

这是Magento的1.7。我可以根据需要提供更多细节。我是Magento开发的初学者,所以任何帮助(包括学习资源)都将不胜感激。谢谢!

回答

1

1)管理区确实有店铺ID = 0,所以是的,这将始终被退回。

这也意味着你的条件总是失败并且从不进行任何连接,它只会尝试将qty_sold添加到集合中,这当然不起作用,因为它不是实体数据的一部分。

问题是joinAttribute方法只适用于“Entites”(它取决于您尝试加入的模型使用的类),并且由于报告/产品集合不是其中之一加入使用这样的方法的另一种方式:

join() or joinLeft() 

有这样的事情:

$collection->getSelect() 
    ->join(
    'customer_entity', 
    'main_table.customer_id = customer_entity.entity_id', 
    array('customer_name' => 'email') 
); 
0

我能够实现我需要的东西(虽然它是很难完美的)感谢安德鲁是尖。这里是我的setCollection更新的代码:

public function setCollection($collection) 
{ 
    /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */ 

    $store = $this->_getStore(); 

    $collection->getSelect()->joinLeft(
     array('oi' => 'sales_flat_order_item'), 
     'e.entity_id = oi.product_id', 
     array("qty_sold" => 'SUM(oi.qty_ordered)') 
    )->group('e.entity_id'); 

    parent::setCollection($collection); 
} 

我很好奇,如果任何Magento的开发商看到这种方法的一个严重缺陷(我所知道的一些业务逻辑,如已取消的不算制品等)或建议更好的方法。无论哪种方式,这是“足够好”,现在满足我的需求,所以我会张贴,以防其他人需要它。

1

也处理这个问题并解决它,如下所示:

protected function _prepareCollection() { 
    // [...] 
    $collection = Mage::getModel('catalog/product')->getCollection(); 

    // Add subquery join to sales_flat_order_item to get a SUM of how many times this product has been ordered 
    $totalOrderedQuery = Mage::getSingleton('core/resource')->getConnection('core_read') 
     ->select() 
     ->from('sales_flat_order_item', array('product_id', 'qty_ordered' => 'SUM(`qty_ordered`)')) 
     ->group('product_id'); 

    $collection->joinField('qty_ordered', $totalOrderedQuery, 'qty_ordered', 'product_id=entity_id', null, 'left'); 
    // [...] 
    return parent::_prepareCollection(); 
} 

$collection->joinField()使用,使用上述$collection->getSelect()->join()试过,但它提供了各种各样的问题与排序顺序和过滤因内部Magento数据收集不会将该列识别为数据集的一部分。

然后在_prepareColumns你可以简单地添加列:

$this->addColumn('qty_ordered', array(
    'header' => Mage::helper('xyz')->__('Total quantity ordered'), 
    'sortable' => true, 
    'width' => '10%', 
    'index' => 'qty_ordered', 
)); 

你可能要添加一个渲染器,检查和转换一个NULL价值'0',否则你将最终空列,如果产品不是招“T尚未订购(你也可以通过使用IFNULLqty_ordered解决这个问题的SELECT查询)。