BElow是选择单个产品的相关产品的功能。我希望它的工作方式是,如果没有相关产品,其他随机产品将被添加到阵列中。 Random可以是同一类别的其他产品,如果同一类别中没有procut,我们可以从其他类别中获取。
protected function _prepareData()
{
$product = Mage::registry('product');
/* @var $product Mage_Catalog_Model_Product */
$this->_itemCollection = $product->getRelatedProductCollection()
->addAttributeToSelect('required_options')
->addAttributeToSort('position', Varien_Db_Select::SQL_ASC)
->addStoreFilter()
;
if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
Mage::getSingleton('checkout/session')->getQuoteId()
);
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
// Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
由于 Abnab
答案 0 :(得分:1)
您可以使用count()查看是否有相关产品。如果没有,那么您可以使用您想要的任何过滤器加载新的产品集合。作为一个例子,我在category_id下面进行了过滤。我建议您阅读Magento collections(或here)。
protected function _prepareData()
{
...
$this->_itemCollection->load();
// If there are no related products, find more products in same category.
if (count($this->_itemCollection) < 1) {
$this->_itemCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('required_options')
->addAttributeToFilter('category_id', $product->getCategoryId());
}
foreach ($this->_itemCollection as $product) {
...