Magento-如何通过产品获得收入
我在和Magento斗争 我在ProductOrdered报告中添加了几个列,我希望有一个列包含该特定产品的总收入 检查此示例: 总收入数字是错误的,我检查了每个订单,但不是那个数字 我做错了什么?代码如下: 站点>应用程序>代码>核心>图像>管理HTML>区块>报告>产品>售出Magento-如何通过产品获得收入,magento,Magento,我在和Magento斗争 我在ProductOrdered报告中添加了几个列,我希望有一个列包含该特定产品的总收入 检查此示例: 总收入数字是错误的,我检查了每个订单,但不是那个数字 我做错了什么?代码如下: 站点>应用程序>代码>核心>图像>管理HTML>区块>报告>产品>售出 class Mage_Adminhtml_Block_Report_Product_Sold_Grid extends Mage_Adminhtml_Block_Report_Grid { /** * Sub rep
class Mage_Adminhtml_Block_Report_Product_Sold_Grid extends Mage_Adminhtml_Block_Report_Grid
{
/**
* Sub report size
*
* @var int
*/
protected $_subReportSize = 0;
/**
* Initialize Grid settings
*
*/
public function __construct()
{
parent::__construct();
$this->setId('gridProductsSold');
}
protected function _afterLoadCollection()
{
$totalObj = new Mage_Reports_Model_Totals();
$this->setTotals($totalObj->countTotals($this));
}
/**
* Prepare collection object for grid
*
* @return Mage_Adminhtml_Block_Report_Product_Sold_Grid
*/
protected function _prepareCollection()
{
parent::_prepareCollection();
$this->getCollection()
->initReport('reports/product_sold_collection');
return $this;
}
protected function _getStore()
{
$storeId = (int) $this->getRequest()->getParam('store', 0);
return Mage::app()->getStore($storeId);
}
/**
* Prepare Grid columns
*
* @return Mage_Adminhtml_Block_Report_Product_Sold_Grid
*/
protected function _prepareColumns()
{
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id');
$collection->addAttributeToSelect('producer_tag');
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$collection->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left');
}
if ($store->getId()) {
//$collection->setStoreId($store->getId());
$adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
$collection->addStoreFilter($store);
$collection->joinAttribute(
'name',
'catalog_product/name',
'entity_id',
null,
'inner',
$adminStore
);
$collection->joinAttribute(
'custom_name',
'catalog_product/name',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'status',
'catalog_product/status',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'price',
'catalog_product/price',
'entity_id',
null,
'left',
$store->getId()
);
}
else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$this->addColumn('name', array(
'header' =>Mage::helper('reports')->__('Product Name'),
'index' =>'order_items_name'
));
$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
->load()
->toOptionHash();
$this->addColumn('set_name',
array(
'header'=> Mage::helper('catalog')->__('Category'),
'width' => '150px',
'index' => 'attribute_set_id',
'type' => 'options',
'options' => $sets,
));
$this->addColumn('producer_tag',
array(
'header'=> Mage::helper('catalog')->__('Producer Tag'),
'width' => '150px',
'index' => 'producer_tag',
'type' => 'text'
));
$store = $this->_getStore();
$this->addColumn('price',
array(
'header'=> Mage::helper('catalog')->__('Price'),
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'price',
));
$this->addColumn('ordered_qty', array(
'header' =>Mage::helper('reports')->__('Quantity Ordered'),
'width' =>'120px',
'align' =>'right',
'index' =>'ordered_qty',
'total' =>'sum',
'type' =>'number'
));
$currencyCode = $this->getCurrentCurrencyCode();
$rate = $this->getRate($currencyCode);
$this->addColumn('revenue',
array(
'header'=> Mage::helper('catalog')->__('Revenue (Only Complete Orders)'),
'type' => 'currency',
'index' => 'price',
'total' => 'sum',
'type' => 'currency',
'currency_code' => $currencyCode,
'rate' => $rate,
));
$this->addExportType('*/*/exportSoldCsv', Mage::helper('reports')->__('CSV'));
$this->addExportType('*/*/exportSoldExcel', Mage::helper('reports')->__('Excel XML'));
return parent::_prepareColumns();
}
}
别明白我做错了什么
我的目标是要有一份报告,说明我用这些产品花了多少钱
欢迎任何帮助或理想
提前感谢。有关我的代码建议的基础,请参阅此处发布的答案 由于您希望在前端显示此值,因此我假设为phtml,因此我将以这种方式对其进行编码,并尝试将最终价格乘以售出数量
<?php
// Enter your product ID here
$id = 123;
// Load the product collection and do some filters
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('id', $id)
->setOrder('ordered_qty', 'desc')
->getFirstItem();
// Since the filter selects one product, we can use it here as the single result
$product = $_productCollection;
// Load the tax helper
$_taxHelper = Mage::helper('tax');
// Get the final price of the product
$finalprice = $_taxHelper->getPrice($product, $product->getFinalPrice(), true);
// Print the results
echo $product->getName() . " total revenue: " . (int)$product->ordered_qty * $finalprice;
?>
这是未经测试的,是动态编码的,但概念非常基本,因此您应该可以毫无困难地调整我所展示的内容,以满足您的需要。在另一个线程中看到了您的答案,但我希望在产品订购的当前报告中这样做,但在我与其他开发人员交谈时,不推荐这样做。我想要一份有日期范围的产品报告,所有销售的产品都要有详细信息。但我正在和一个开发者核实这一点。非常感谢你的回复!这是毫无意义的,如果最终价格发生变化,这对每个人来说都是有益的。