且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento:如何检索(即)分组产品的最低价格?

更新时间:2023-11-07 13:17:10

我更深入地研究了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.