我对magento很新,请看下面的代码我试图在不同的基础上重新安排productCollection。
$this->_productCollection = $layer->getProductCollection();
$rearraggedProductCollection=array();
foreach($this->_productCollection as $product)
{
if($product->getTypeId()=="simple")
{
array_push($rearraggedProductCollection,$product);
}
}
$this->_productCollection = $rearraggedProductCollection;
但这不起作用,任何人都会感激不尽。
答案 0 :(得分:1)
为什么不直接对集合应用过滤器?
$this->_productCollection->addAttributeToFilter('type_id', array('eq' => 'simple'))
答案 1 :(得分:0)
我认为你要找的是PHP Clone。
foreach($this->_productCollection as $product) {
if($product->getTypeId()=="simple") {
array_push($rearraggedProductCollection, clone $product);
}
}
我怀疑以下声明,我不确定你是否可以重置这样的对象。
$this->_productCollection = $rearraggedProductCollection;
您能否确认您的阵列设置正确,处理像数组这样的对象会导致一些时髦的行为。
每个阶段var_dump并尝试确定错误的地方
答案 2 :(得分:0)
谢谢大家帮我解决这个问题。
我找到了soloution,它很简单,并且有一个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;