2012-11-16 15 views
0

我對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

你有錯誤?如果是,它是什麼? –

+0

不,我沒有得到任何錯誤,我認爲是陣列和對象類型之間的類型不匹配,我很困惑... –

+0

好吧,我會檢查它 –

回答

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); 

,並同時增加產品的空表我們需要舒爾檢查產品標識不重複,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; 
1

爲什麼你不只是應用過濾器的集合?

$this->_productCollection->addAttributeToFilter('type_id', array('eq' => 'simple')) 
+0

其實我不想篩選產品我想排序它但有很多條件,我想根據我的要求排序第一個產品目錄將顯示延期產品第一>>然後可用>>之後,銷售完產品。因爲這個原因,我正在拿起產品並將其保存在一個陣列中。 –

0

我覺得你要找的是PHP Clone

foreach($this->_productCollection as $product) { 

    if($product->getTypeId()=="simple") { 
     array_push($rearraggedProductCollection, clone $product); 
    } 

} 

我懷疑以下聲明,我不確定您是否可以重置這樣的對象。

$this->_productCollection = $rearraggedProductCollection; 

你能否確認你的數組設置是否正確,處理像數組這樣的對象會導致一些奇怪的行爲。

var_dump每個階段,並嘗試針點往哪裏去錯誤

相關問題