Magento:如何检索(即)分组产品的最低价格? [英] Magento: How to retrieve the minimal price of (i.e.) grouped products?

查看:91
本文介绍了Magento:如何检索(即)分组产品的最低价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Magento 1.7.0.2的产品视图页面上显示分组产品的价格,就像在类别产品列表中显示该价格一样(起始于:xx.xx").我以为我可以用

I am trying to display a grouped product's price on the product view page in Magento 1.7.0.2, just as it is being displayed in the category product listing ("Starting at: xx.xx"). I thought I could just use

$this->getPriceHtml($_product, true);

这样做是因为它在类别产品清单中的工作方式相同,但是正如我在Magento中尝试做的所有事情一样,这并不容易,因为该方法不会在产品视图页面上返回任何内容.

to do so because it works the same way in the category product listing, but as everything I tried to do in Magento, it is not that easy because that method doesnt return anything on the product view page.

我对Magento的代码进行了更深入的研究,发现导致此问题的原因是未在产品视图页面上设置产品的最低价格数据($ _product-> getMinimalPrice()返回null).

I looked a little deeper into Magento's code and figured out that the cause of this problem is the product's minimal price data not being set on the product view page ($_product->getMinimalPrice() returns null).

因此,我对如何加载产品的最低价格进行了一些研究,提出了诸如以下的想法

Therefore I did some research on how to load a product's minimal price which brought up ideas like

$_product->getPriceModel()->getMinimalPrice($_product);

无效,因为显然该方法已在最近的更新之一中被弃用并删除,或者

which doesnt work because apparently that method was deprecated and removed in one of the last updates or

$priceModel = Mage::getResourceModel('catalogindex/price');
$priceModel->setStoreId(Mage::app()->getStore()->getId());
$priceModel->setCustomerGroupId(Mage::getSingleton('customer/session')->getCustomerGroupId());

$minimalPrices = $priceModel->getMinimalPrices(array($_product->getId()));
$minimalPrice = $minimalPrices[0];
$_product->setData('minimal_price', $minimalPrice['value']);
$_product->setData('minimal_tax_class_id', $minimalPrice['tax_class_id']);

由于表'catalogindex_minimal_price'为空,因此无效.

which doesnt work either because the table 'catalogindex_minimal_price' is empty.

所以我的问题是,Magento如何在类别产品列表中加载最低价格?

So my question is, how does Magento load the minimal price in the category product listing?

推荐答案

我更深入地研究了Magento的代码,并追踪了Magento如何加载产品列表中的产品. 查看产品列表时,产品集合由模型Mage_Catalog_Model_Layer加载,该模型通过向集合的SQL中添加联接将最小价格和其他东西添加到集合中的产品中,但是我还没有找到执行相同操作的模型单一产品而不是集合的东西.

I looked a little deeper into Magento's code and tracked down how Magento loads the products of the product list. When viewing the product list, the product collection is loaded by the model Mage_Catalog_Model_Layer, which adds the minimal price and other stuff to the products in the collection by adding joins to the collection’s SQL, but I have not yet found a model that does the same thing for single products instead of collections.

由于我不想使用直接的SQL查询,因此我检出了CatalogIndex模块,但是该模块似乎根本无法工作,因为其所有索引表都是空的,甚至已损坏.

Because I don’t want to use a direct SQL query, I checked out the CatalogIndex module, but that module seems not to be working at all because all its indexing tables are empty or even corrupted.

$data_grouped = Mage::getModel("catalogindex/data_grouped");
$minimal = $data_grouped->getMinimalPrice(array($_product->getId()), Mage::app()->getStore());

起初看起来不错,但它给了我以下错误:

looked good at first but it gives me the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_index_price.value' in 'field list'

不推荐使用CatalogIndex模块,或者我太愚蠢而无法启用它.

The CatalogIndex module is either deprecated or I’m just too stupid to enable it.

但是,我现在在price.phtml模板中创建了一种解决方法,该模板使用与产品列表相同的方法来检索最低价格和税率: 在模板的开头,我添加了以下内容:

However, I have now created a workaround in my price.phtml template, which uses the same method to retrieve the minimal price and tax percents as the product list does: At the beginning of the template I added the following:

$this->setProduct(
    Mage::getModel("catalog/product")->getCollection()
        ->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
        ->addAttributeToFilter("entity_id", $this->getProduct()->getId())
        ->setPage(1, 1)
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->load()
        ->getFirstItem()
);

这可能不是完美的解决方案,但它可以完成工作,并且比迭代所有子产品并以此方式找到最低价格要干净一些.

which may not be the perfect solution, but it does the job and it does it a little cleaner than iterating over all child products and finding the minimal price that way.

这篇关于Magento:如何检索(即)分组产品的最低价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆