【问题标题】:how do I sort products by most sold in desc order based on reports?我如何根据报告按最畅销的顺序对产品进行排序?
【发布时间】:2016-01-13 11:02:14
【问题描述】:

我想使用报告根据最畅销的数量对类别中的产品进行排序。我尝试了下面的代码,但它只显示已售出的产品,而不是未售出的产品。我想知道如何按 desc 顺序在产品集合中同时获得已售出和未售出的产品。谢谢

    if (Mage::helper('abc_xyz')->displayMostSoldProducts()) {               
    $collection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addOrderedQty()               
    ->addCategoryFilter($category)
    ->setOrder('ordered_qty', 'desc');
}

【问题讨论】:

    标签: magento magento-1.9 magento-1.8


    【解决方案1】:

    向产品集合发送查询时,您使用的是标准方法。反过来,他们重写 Select 并使其使用 sales_flat_quote_item 表中的数据。您可能知道,此表不存储已售产品的数据。

    因此,您需要重写 Select 您想要的方式。应该是这样的:

    $collection = Mage::getResourceModel('reports/product_collection');
    $select = $collection->getSelect();
    $select->joinLeft(
        array('sfoi' => $collection->getTable('sales/order_item')),
        '`e`.`entity_id` = `sfoi`.`product_id` ',
        array(
            'ordered_qty' => 'SUM(sfoi.qty_ordered)',
            'order_items_name' => 'sfoi.name',
            'entity_id' => 'sfoi.product_id',
        ))
    ->where('sfoi.parent_item_id IS NULL')
    ->group('e.entity_id');
    

    就我而言,我已经获取了集合本身并添加了所有必要的信息。

    结果,我得到了这个: https://gyazo.com/4a0b02298173f2c9356b7d7df783c0ff

    这包括所有未售出的产品: https://gyazo.com/24859373f52d31dba50560a02e0d0d79

    但请注意,在解决此问题时,您应该考虑到您案件的所有特点。不幸的是,没有任何标准的方法来实现这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多