检查此解决方案:
我发现可配置产品的销售额被正确汇总,但未包含在结果中;他们的子产品会出现。我的解决方案是包含可配置产品,在表上进行左连接catalog_product_super_link
,并过滤掉任何具有parent_id
. 以下是您需要进行的更改:
集合.php:
public function addOrderedQty($from = '', $to = '', $getComplexProducts=false, $getComplexChildProducts = true, $getRemovedProducts = true)
{
$qtyOrderedTableName = $this->getTable('sales/order_item');
$qtyOrderedFieldName = 'qty_ordered';
$productIdFieldName = 'product_id';
if (!$getComplexProducts) {
$compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
$productTypes = $this->getConnection()->quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);
} else {
$productTypes = '';
}
if ($from != '' && $to != '') {
$dateFilter = " AND `order`.created_at BETWEEN '{$from}' AND '{$to}'";
} else {
$dateFilter = "";
}
$this->getSelect()->reset()->from(
array('order_items' => $qtyOrderedTableName),
array(
'ordered_qty' => "SUM(order_items.{$qtyOrderedFieldName})",
'order_items_name' => 'order_items.name'
)
);
$_joinCondition = $this->getConnection()->quoteInto(
'order.entity_id = order_items.order_id AND order.state<>?', Mage_Sales_Model_Order::STATE_CANCELED
);
$_joinCondition .= $dateFilter;
$this->getSelect()->joinInner(
array('order' => $this->getTable('sales/order')),
$_joinCondition,
array()
);
// Add join to get the parent id for configurables
$this->getSelect()->joinLeft(
array('cpsl' => $this->getTable('catalog/product_super_link')),
'cpsl.product_id = order_items.product_id',
'cpsl.parent_id'
);
if(!$getComplexChildProducts)
$this->getSelect()->having('parent_id IS NULL');
if($getRemovedProducts)
{
$this->getSelect()
->joinLeft(array('e' => $this->getProductEntityTableName()),
"e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
->group('order_items.product_id');
}
else
{
$this->getSelect()
->joinInner(array('e' => $this->getProductEntityTableName()),
"e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
->group('e.entity_id');
}
$this->getSelect()->having('ordered_qty > 0');
// This line is for debug purposes, in case you'd like to see what the SQL looks like
// $x = $this->getSelect()->__toString();
return $this;
}
List.php - 找到以下两行...
$bestsellers->addOrderedQty($startDate, $todayDate, true);
$bestsellers->addOrderedQty('', '', true);
...并将它们更改为:
$bestsellers->addOrderedQty($startDate, $todayDate, true, false, false);
$bestsellers->addOrderedQty('', '', true, false, false);
我的更改添加了两个新的可选参数,它们都默认为true
,以免破坏现有功能。
- 当
$getComplexChildProducts
设置为false
时,可配置产品的所有子项将从结果中删除。
$getRemovedProducts
确定以前订购的产品(已从 Magento 中删除)是否也应出现。
请注意,您的报告统计数据需要保持最新才能获得准确的结果。
来源和致谢:https ://stackoverflow.com/a/8419579/1136132