2011-12-13 140 views
0

Hy ,,我試圖讓Magento通過將所有產品用相同的類別進行分組並比較它們並將它們組合在一起進行比較,比較所有他們togher,我已經試過這個代碼加入產品對比magento使用產品屬性(類別)

 collection.php 

     class Mage_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Collection_Abstract 

     public function addCategoryFilterH($request) 
     { 

     $this->_productLimitationFilters['category_id'] =$request; 
     unset($this->_productLimitationFilters['category_is_anchor']); 


     $this->_applyZeroStoreProductLimitations(); 

     $this->_applyProductLimitations(); 

      return $this; 


} 

那我就在產品的比較:

  list.phtml 
      $this_original=$this; 
      $cat15=$this->getItems()->addCategoryFilterH(15); 
      ...for loop throw this products .... 
      $this=$this_original; 
      $cat8=$this->getItems()->addCategoryFilterH(8); 
      ...for loop throw this products .... 
      ...next category id ... 
      ...for loop ... etc 

,但我的代碼沒有工作,任何幫助?

+0

你有一些頁面,有多個類別或什麼?或者您顯示具有多個子類別的類別? –

+0

我只是想覆蓋產品比較的默認功能,因此它可以按其類別顯示比較產品 –

+1

這意味着什麼現在的工作。請更具體地說明?你檢查過日誌嗎? –

回答

0

在collection.php添加此功能,你不需要寫類名和我一樣,我寫它確保您正在修改正確的文件

class Mage_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Collection_Abstract 

    public function addCategoryFilterH($request) 
    { 

    $this->_productLimitationFilters['category_id'] =$request; 
    unset($this->_productLimitationFilters['category_is_anchor']); 


    $this->_applyZeroStoreProductLimitations(); 

    $this->_applyProductLimitations(); 

     return $this; 

在list.php的「應用程序/設計/前端/基/默認/模板/目錄/產品/比較/ list.phtml」 在你的頂部添加此代碼文件

<?php 
$thiscount=clone $this; 
$this1=clone $this; 
$comparecats=array(); 
?> 
<?php foreach($thiscount->getItems() as $_item): ?> 
     <?php 


$att=$_item->getCategoryIds(); 
array_push($comparecats,$att[0]); 

?> 
    <?php endforeach; 

echo "<br>"; 

?> 

<?php 

$comparecats = array_unique($comparecats); 

$this2= clone $this; 

?> 

<?php foreach ($comparecats as $cateid): 
unset($this); 
$this2=clone $this1; 
$this2->getItems()->addCategoryFilterByCategoryId($cateid); 
echo "<br>"; 
?> 
.........for loop your products using this ............... 
0

我只有一個想法。通過Store Manager for Magento導入產品到csv中,然後進行比較。我不確定你的代碼對於這個動作來說是相當不錯的。

+0

產品比較適用於使用我的網站的用戶,,,不適用於系統管理員 –

1
yes ,, it give me the same output .. filter only work for the first time .. 

嘗試重新加載集合。類似的問題可能是here

$collection->clear(); 
$collection->....//here you add some logic for filtering; 
$collection->load();//here collection with new filters will be loaded. 

更新1

所以我的建議是建立在你的塊的其他的方法,該方法重置集合。甚至可以將重置添加到您的類別加載功能。那麼這取決於你。

+1

具體而言,'$ this_original = $ this'不會創建'$ this'的備份副本,它只會爲其創建第二個引用。 – clockworkgeek

+0

哦,是啊!偉大的加法clockworkgeek! –

+0

所以我怎麼可以做一個副本? –

0

在collection.php添加此功能,您不需要像我一樣寫類名,我寫它確保你正在修改正確的文件

class Mage_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Collection_Abstract 

    public function addCategoryFilterH($request) 
    { 

    $this->_productLimitationFilters['category_id'] =$request; 
    unset($this->_productLimitationFilters['category_is_anchor']); 


    $this->_applyZeroStoreProductLimitations(); 

    $this->_applyProductLimitations(); 

     return $this; 
在list.php的

「應用程序/設計/前端/基/默認/模板/目錄/產品/比較/ list.phtml」 在你的頂部添加此代碼文件

<?php 
$thiscount=clone $this; 
$this1=clone $this; 
$comparecats=array(); 
?> 
<?php foreach($thiscount->getItems() as $_item): ?> 
     <?php 


$att=$_item->getCategoryIds(); 
array_push($comparecats,$att[0]); 

?> 
    <?php endforeach; 

echo "<br>"; 

?> 

<?php 

$comparecats = array_unique($comparecats); 

$this2= clone $this; 

?> 

<?php foreach ($comparecats as $cateid): 
unset($this); 
$this2=clone $this1; 
$this2->getItems()->addCategoryFilterByCategoryId($cateid); 
echo "<br>"; 
?> 
.........for loop your products using this ...............