Php Magento展示制造商的最新产品
我想从一个特定的制造商那里得到最新的3种产品。我想做的地方是一个产品页面,所以我必须找到与产品相关的制造商,并显示最新的产品 这是我到目前为止得到的代码,它可以工作,但显示随机乘积取决于我输入的编号Php Magento展示制造商的最新产品,php,magento,magento-1.7,Php,Magento,Magento 1.7,我想从一个特定的制造商那里得到最新的3种产品。我想做的地方是一个产品页面,所以我必须找到与产品相关的制造商,并显示最新的产品 这是我到目前为止得到的代码,它可以工作,但显示随机乘积取决于我输入的编号->addAttributeToFilter() ->addAttributeToFilter()是否确实能够筛选制造商?如果没有,还应该用什么来让它工作 <?php $_productCollection = Mage::getResourceModel('reports/product_co
->addAttributeToFilter()
->addAttributeToFilter()
是否确实能够筛选制造商?如果没有,还应该用什么来让它工作
<?php $_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
//->addAttributeToFilter('manufacturer', 23)
->addAttributeToFilter(array(array('attribute'=> 'manufacturer', 18)))
->setVisibility(array(2,3,4))
->setOrder('created_at', 'desc')
->setPage(1, 3); ?>
<?php foreach($_productCollection as $_product) : ?>
<li class="arrowksleeper">
<div class="menugridprodcont">
<div><a href="<?php echo $_product->getProductUrl(); ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 100); ?>" alt="" /></a></div>
<div id="menugridprodtitle"><a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_product->getName(); ?>"><?php echo $_product->getName(); ?></a></div>
</div>
</li>
<?php endforeach; ?>
谢谢。addAttributeToFilter通过传递属性名称,然后传递一个描述如何对其进行过滤的数组来工作(除非传递的不是数组,而是
'null'
或'notnull'
)。你可能想要更像这样的东西
addAttributeToFilter('manufacturer', array('eq' => 18));
完整的工作代码。这假定“18”是制造商的有效属性。实际上,您可以通过检查Catalog->Attributes->ManageAttributes->Manufacturer->Labels的表单来获取这些信息
$col = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', array('eq'=> 18))
->setOrder('created_at', 'desc')
->setPage(1, 3);
为什么要使用报表集合?如果不加载制造商属性,请使用主产品集合 试试这个:
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToSelect('manufacturer');
//$collection->addFieldToFilter(array(
// array('name' => 'manufacturer', 'eq' => 'ibm'),
//));
$collection->addAttributeToFilter('manufacturer', array('eq' => 99));
$collection->setOrder('created_at', 'desc');
$collection->setCurPage(1)->setPageSize(5); // first 5 products..
foreach($collection as $product) {
// ..
}
非常感谢安德鲁和杰瑞德花时间帮助我。不幸的是,他们提供的脚本对我不起作用(这并不意味着他们的脚本对你不起作用),所以我也在下面发布,我是如何管理它的 为了工作,下面的脚本应该放在
app/design/frontend/default/YOUR-THEME/template/catalog/product/view.phtml
<?php
$manuId = $_product->getManufacturer(); // getting manufacturer's id
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('manufacturer', array('eq'=> $manuId)) // $manuId: printing manufacturer's id
->setVisibility(array(2,3,4))
->setOrder('created_at', 'desc')
->setPage(1, 3); ?>
<?php foreach($_productCollection as $_product) : ?>
<div>
<div><a href="<?php echo $_product->getProductUrl(); ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 100); ?>" alt="" /></a></div>
<div><a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_product->getName(); ?>"><?php echo $_product->getName(); ?></a></div>
</div>
<?php endforeach; ?>
我以前也试过,但没什么效果,它给我带来了一些随机的东西,而且它没有错。所以这里还有别的地方不对劲。在foreach之前,您可以回显或出错\u log$\u productCollection->getSelect()代码>?然后您可以看到SQL实际上是如何运行的,并找出它是随机的原因。$\u productCollection->getSelect();在foreach对你没有好处之前,我不会在此时组装查询。安德鲁,你错了。->getSelect()将基于前面的过滤器、选择、订单等构建查询。请尝试添加echo$col->getSelect()代码>在我上面的代码之后。您将得到一个如下所示的字符串(SQL)<代码>在('at_manufacturer.'entity_id'='e.'entity_id')和('at_manufacturer.'attribute_id'='102')和('at_manufacturer.'store_id'=0)上选择'e.*,'at_manufacturer.'value'作为'manufacturer'实体'e'和'e'中的'value'作为'manufacturer'
我之所以使用它,是因为不幸的是,对于产品系列,它不起作用。。事实上,您的脚本在314行的app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php中给出了一个500错误ERR(3):注意:未定义索引:attribute in app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php