谢谢大家帮我解决这个问题。
我找到了解决方案,它很简单,并且有一个 addItem() 可以轻松帮助将新产品添加到 _productCollection 对象。
我唯一的窍门是加载一个空集合
//load an empty collection (filter-less collections will auto-lazy-load everything)
$merged = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id',-1);
并且在将产品添加到空表时,我们需要检查产品 ID 不重复,magento 不允许将重复的产品添加到产品集合中
if(!$merged->getItemById($product->getId()))
{
$merged->addItem($product);
}
根据我的要求,整个代码如下
$this->_productCollection = $layer->getProductCollection();
//load an empty collection (filter-less collections will auto-lazy-load everything)
$merged = Mage::getModel('catalog/product')->getCollection()->addFieldToFilter('entity_id',-1);
/**** Sorting the backorder product first ****/
foreach($this->_productCollection as $product)
{
if($product->getTypeId()=="simple" && $product->isSaleable())
{
if(!$merged->getItemById($product->getId()))
{
$merged->addItem($product);
}
}
}
/**** Sorting the avilable product second ****/
foreach($this->_productCollection as $product)
{
if($product->getTypeId()=="configurable" && $product->isSaleable())
{
if(!$merged->getItemById($product->getId()))
{
$merged->addItem($product);
}
}
}
/**** Sorting the sold out product last ****/
foreach($this->_productCollection as $product)
{
if(!$product->isSaleable())
{
if(!$merged->getItemById($product->getId()))
{
$merged->addItem($product);
}
}
}
/* Now assigning to the mergerd collection to the product Collection */
$this->_productCollection = $merged;