0

好的,我正在运行 Magento 1.7,我的老板要求我在网站顶部设置一个栏,显示库存商品的总数和所述库存的总价值。

到目前为止,我已经算出了项目部分的总数。我对 PHP 不是很好,所以经历了很多试验和错误。

这是我的代码到目前为止的样子[更新:有一个过滤器可以排除缺货产品]:

    <?php  
    $collection = Mage::getModel('catalog/product')->getCollection()
->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
)
->addAttributeToFilter('qty', array('eq' <= 0));
    $_coreHelper = $this->helper('core');


    $sum = 0; 

        foreach ($collection as $product){ 
    $sum += $product->getPrice();
    }
?>

As of today our inventory consists of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $collection->count(); ?></span> items with a total value of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $_coreHelper->currency($sum); ?></span>

总和部分显然不起作用。

谁能给我一些关于如何做到这一点的指示。我假设您将使用循环加载所有产品价格,然后将它们加在一起并显示总数,但正确到达那里对我来说是个问题。

任何帮助,将不胜感激。

4

2 回答 2

0

是的,你可以喜欢这样:

<?php $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('status','1');
$sum = 0;
$count = 0;
foreach ($collection as $_product) {
    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
    if ($stock>=1) {
         $sum+=$_product->getPrice()*$stock;
         $count+=$stock;
    }
}
echo "You have $count items for a total value of ".Mage::helper('core')->currency($sum);
于 2013-02-17T10:40:18.543 回答
0

我对上面 dagfr 的回答有一些问题,getPrice() 在管理仪表板上执行时未能返回任何值,

添加:

$collection->addAttributeToSelect('price');

然后允许我通过以下方式访问价格

$_product['price']
于 2014-01-27T14:37:00.227 回答