この製品が販売された回数を表示するために、製品グリッド (明確にするために管理領域) に列を追加しようとしています。他のいくつかの投稿からつなぎ合わせた後、これまでのところ私が持っているものは次のとおりです。
app/code/local/Namespace/Qtysold/Block/Adminhtml/Catalog/Product/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();
そして、このレポート/製品コレクションに「参加」する方法があるかどうか、またはその「addOrderedQty」データを再作成する方法があるかどうかはわかりませんか?
これはMagento 1.7にあります。必要に応じて、さらに詳細を提供できます。私は Magento 開発の初心者なので、(学習するためのリソースを含め) どんな助けでも大歓迎です。ありがとう!