Magento:如何检索(即)分组产品的最低价格?
我试图在Magento 1.7.0.2中的产品视图页面上显示分组产品的价格,就像在类别产品列表中显示一样(“从:xx.xx开始”)。我想我可以用Magento:如何检索(即)分组产品的最低价格?,magento,Magento,我试图在Magento 1.7.0.2中的产品视图页面上显示分组产品的价格,就像在类别产品列表中显示一样(“从:xx.xx开始”)。我想我可以用 $this->getPriceHtml($_product, true); 这样做是因为它在类别产品列表中的工作方式相同,但正如我在Magento中尝试的所有操作一样,这并不容易,因为该方法不会在产品视图页面上返回任何内容 我深入研究了Magento的代码,发现问题的原因是产品的最低价格数据没有在产品视图页面上设置($\u product->g
$this->getPriceHtml($_product, true);
这样做是因为它在类别产品列表中的工作方式相同,但正如我在Magento中尝试的所有操作一样,这并不容易,因为该方法不会在产品视图页面上返回任何内容
我深入研究了Magento的代码,发现问题的原因是产品的最低价格数据没有在产品视图页面上设置($\u product->getMinimalPrice()返回null) 因此,我做了一些关于如何加载产品的最低价格的研究,提出了如下想法
$_product->getPriceModel()->getMinimalPrice($_product);
这不起作用,因为显然该方法在最近的一次更新或更新中被弃用并删除
$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\u minimal\u price”表是空的,所以也不起作用
因此,我的问题是,Magento如何在类别产品列表中加载最低价格?我深入研究了Magento的代码,并跟踪了Magento如何加载产品列表中的产品。 查看产品列表时,产品集合由model Mage_Catalog_model_层加载,该层通过向集合的SQL中添加连接,将最低价格和其他内容添加到集合中的产品中,但我尚未找到一个对单个产品而不是集合执行相同操作的模型 因为我不想使用直接SQL查询,所以我签出了CatalogIndex模块,但该模块似乎根本无法工作,因为它的所有索引表都是空的,甚至已损坏
$data_grouped = Mage::getModel("catalogindex/data_grouped");
$minimal = $data_grouped->getMinimalPrice(array($_product->getId()), Mage::app()->getStore());
起初看起来不错,但它给了我以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_index_price.value' in 'field list'
CatalogIndex模块已弃用,或者我太笨了,无法启用它
但是,我现在在price.phtml模板中创建了一个变通方法,它使用与产品列表相同的方法来检索最低价格和税收百分比:
在模板的开头,我添加了以下内容:
$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()
);
这可能不是一个完美的解决方案,但它确实起到了作用,而且比迭代所有子产品并以这种方式找到最低价格要干净一点。这不是一个答案,更像是对Subsurf答案的一个大的HTML注释。如果您正在阅读本文,您可能正在实现自己的自定义模块以显示产品列表。在我的情况下,这是一个单一的页面只为批发商。我的目标是得到一份我自己公司产品的清单,并给他们一份清单,就像它是另一个类别一样。我将模板的
list.phtml
复制到新的content.phtml
。但是getMinimalPrice()
返回了NULL
,这意味着当此代码调用getPriceHtml()
时,price.phtml
没有显示批发价格
我用索引控制器和content.phtml做了一个简单的模块。使用我在网上看到的样板文件,在indexController.php
文件中,更改:
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'b2b',
array('template' => 'b2b/content.phtml')
);
致:
这对您正确显示产品列表有很大帮助
现在,关于模板文件,在我的例子中是
content.phtml
,您获得产品集合,然后在foreach()
顶部使用Subsurf的代码,该代码重复
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
// ->addAttributeToSelect('*')
// doesn't work ->addAttributeToSelect('special_price')
->addAttributeToSelect('sku')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
// http://stackoverflow.com/questions/1332742/magento-retrieve-products-with-a-specific-attribute-value
->addFieldToFilter( array(
array('attribute'=>'manufacturer','eq'=>'143')
, array('attribute'=>'manufacturer','eq'=>'139')
))
;
echo "<p>There are ".count($_productCollection)." products: </p>";
echo '<div class="category-products">';
// List mode
if($this->getMode()!='grid') {
$_iterator = 0;
echo'<ol class="products-list" id="products-list">';
foreach ($_productCollection as $_product) {
?> <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
<?php
$_product=Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
->addAttributeToFilter("entity_id", $_product->getId())
->setPage(1, 1)
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->load()
->getFirstItem()
);
echo "Minimal price: ".$_product->getMinimalPrice()."<br>\n";
$\u productCollection=Mage::getModel('目录/产品')
->getCollection()
//->addAttributeToSelect(“*”)
//不起作用->添加属性选择(“特价”)
->addAttributeToSelect('sku')
->addAttributeToFilter('状态',1)
->addAttributeToFilter('visibility',4)
// http://stackoverflow.com/questions/1332742/magento-retrieve-products-with-a-specific-attribute-value
->addFieldToFilter(阵列(
数组('attribute'=>'manufacturer','eq'=>'143')
,数组('attribute'=>'manufacturer','eq'=>'139')
))
;
echo“有”。计数($\u productCollection)。“产品:”;
回声';
//列表模式
如果($this->getMode()!='grid'){
$\u迭代器=0;
回声';
foreach($\产品集合作为$\产品){
?>还有另一种方法允许您在模板中使用$this->getPriceHtml($\u product,true);
,然后处理所有复杂的价格显示规则,并将“开始于”放在分组产品价格之前
我将其用于定制特色产品块。在块类中,我具有以下功能:
class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract {
public function setGroupedProductPrice($group_product) {
$prices = array();
foreach ($group_product->getTypeInstance()->getChildrenIds($group_product->getId()) as $ids) {
foreach ($ids as $id) {
$product = Mage::getModel('catalog/product')->load($id);
$prices[] = $product->getPriceModel()->getPrice($product);
}
}
ksort($prices);
$group_product->setPrice(array_pop($prices));
}
}
然后在模板中:
<?php
if ($_product->getTypeId() == 'grouped')
$this->prepareGroupedProduct($_product);
echo $this->getPriceHtml($_product, true);
?>
以下内容在不使用产品集合的情况下实现了这一技巧:
$priceResource = Mage::getResourceSingleton('catalogindex/price');
$select = $priceResource->getReadConnection()->select();
$select->from(
array('price_table' => $priceResource->getMainTable())
)
->where('price_table.entity_id = ?', $_product->getId())
->where('price_table.website_id = ?', Mage::app()->getStore()->getWebsiteId())
->where('price_table.customer_group_id = ?', Mage::getSingleton('customer/session')->getCustomerGroupId());
$result = $priceResource->getReadConnection()->fetchRow($select);
此代码改编自\Mage\u CatalogIndex\u Model\u Resource\u Price::getMinimalPrices
,并适用于单个产品。我做了更多的研究。查看产品列表时,产品集合加载到Mage\u Catalog\u Model\u层,该层通过向集合添加联接来添加最低价格和其他内容n的SQL,但我还没有找到一个对单个产品执行相同操作的模型。因为我真的不想使用直接SQL查询,我签出了CatalogIndex模块,但该模块似乎根本无法工作,因为它的所有索引表都是空的。该模块要么已被弃用,要么我太蠢了,无法启用它。这个工作我在一个更独立的脚本中运行这个,我必须使用setCurrentStore
或setStoreId
手动设置存储。如果不设置它,它默认为admin store,它没有定价信息。Magento的查询执行产品到定价表的内部联接,而不是外部联接,因此addMinimalPrice
神秘地导致查询结果为零产品。
$priceResource = Mage::getResourceSingleton('catalogindex/price');
$select = $priceResource->getReadConnection()->select();
$select->from(
array('price_table' => $priceResource->getMainTable())
)
->where('price_table.entity_id = ?', $_product->getId())
->where('price_table.website_id = ?', Mage::app()->getStore()->getWebsiteId())
->where('price_table.customer_group_id = ?', Mage::getSingleton('customer/session')->getCustomerGroupId());
$result = $priceResource->getReadConnection()->fetchRow($select);