1

この製品が販売された回数を表示するために、製品グリッド (明確にするために管理領域) に列を追加しようとしています。他のいくつかの投稿からつなぎ合わせた後、これまでのところ私が持っているものは次のとおりです。

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 開発の初心者なので、(学習するためのリソースを含め) どんな助けでも大歓迎です。ありがとう!

4

3 に答える 3

1

1) 管理エリアにはストア ID = 0 があるため、常に返されます。

これはまた、条件が常に失敗し、決して結合しないことを意味します。qty_sold をコレクションに追加しようとするだけです。これは、そのエンティティ データの一部ではないため、もちろん機能しません。

問題は、joinAttribute メソッドが「エンティティ」でのみ機能することです (結合しようとしているモデルで使用されるクラスによって異なります)。レポート/製品コレクションはこれらのいずれでもないため、別の方法で結合する必要があります。次のような方法を使用します。

join() or joinLeft()

この種のもので:

$collection->getSelect()
    ->join(
    'customer_entity',
    'main_table.customer_id = customer_entity.entity_id',
     array('customer_name' => 'email')
 );
于 2013-02-13T17:24:32.077 に答える
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'場合があります。そうしないと、製品がまだ注文されていない場合に空の列になってしまいます ( IFNULL forを使用して SELECT クエリでこれを修正することもできますqty_ordered)。

于 2015-08-27T13:25:55.320 に答える
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 開発者がこのアプローチに重大な欠陥があるかどうか (キャンセルされた製品をカウントしないなどのビジネス ロジックの一部を認識しています)、またはより良いアプローチの推奨事項があるかどうかに興味がありました。いずれにせよ、これは今のところ「十分」であり、私のニーズを満たしているので、他の人が必要とする場合に備えて投稿します.

于 2013-02-13T19:31:38.713 に答える