2010-07-15 151 views
4

這應該應該是愚蠢的事情,但它讓我瘋狂!Magento中的新產品

我想要的只是展示指定類別的新產品,但我所得到的是所有類別的新產品。

假如我要顯示的類別74,我已經試過了代碼

{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" category_id="74" template="catalog/product/new.phtml"}} 

的幾乎任何組合直接分配

{{block type="catalog/product_new" category_id="74" template="catalog/product/new.phtml"}} 

隨着類別根據隱或類別CATEGORY_ID我正在訪問

{{block type="catalog/product_new" template="catalog/product/new.phtml"}} 

此代碼已經過測試,硬編碼在ho我的頁面代碼,創建一個靜態塊,並通過類別面板(靜態塊和產品)包括它......但毫無價值。我已經測試了在這個和其他論壇中找到的幾乎所有代碼,但沒有結果。

不管任何代碼(分配category_id =「74」而不是),我總是得到相同的結果。類別過濾器不起作用,向我展示來自任何類別的新產品,而不是當前類別的新產品,既不是手寫代碼類別的產品(例如category_id =「74」)

另一方面,如果我使用的產品列表,而不是新產品,工作在主頁和靜態塊好了,種類似乎也創造

{{block type="catalog/product_list" category_id="74" template="catalog/product/list.phtml"}} 

這表明,屬於類別74

我使用我的產品Mgento Magento ver。 1.3.2.4,即使使用不同的模板也可以顯示相同的結果。

任何意見都會受到歡迎

有一個愉快的一天,J.

PS我也試圖與其他類別,不僅74.父類,兒童類...但沒有結果在所有

回答

1

catalog/product_new塊不接受category_id作爲參數。如果您想爲單個類別執行此操作,則需要創建自己的自定義塊,該塊從catalog/product_new下降,並覆蓋方法_beforeToHtml以使用category_id。

希望有幫助!

謝謝, 喬

+0

你能更具體嗎?作爲一名程序員,我瞭解您在談論重寫_beforeToHtml方法時的含義,但我不是一名PHP程序員,並且我對此語言的瞭解有限。 我猜我應該修改的文件是Core文件app \ code \ core \ Mage \ Catalog \ Block \ Product \ New.php,不是嗎? 下面是我的代碼,你能幫我完成嗎? 感謝您的幫助,J. – juankomik 2010-07-16 08:41:28

+0

請您解釋如何通過Category ID檢索新產品(即逗號分隔=> 3,6,7)... – 2011-06-30 05:37:27

0

這對我的作品。

protected function _beforeToHtml() 
    { 
     $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); 

     $collection = Mage::getResourceModel('catalog/product_collection'); 
     $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); 

     $collection = $this->_addProductAttributesAndPrices($collection) 
      ->addStoreFilter() 
      ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate)) 
      ->addAttributeToFilter('news_to_date', array('or'=> array(
       0 => array('date' => true, 'from' => $todayDate), 
       1 => array('is' => new Zend_Db_Expr('null'))) 
      ), 'left') 
      ->addAttributeToSort('news_from_date', 'desc') 
      ->setPageSize($this->getProductsCount()) 
      ->setCurPage(1) 
     ; 


     if($categoryId=$this->getCategoryId()){ 
     $category = Mage::getModel('catalog/category')->load($categoryId); 
     $collection->addCategoryFilter($category); 
} 

     $this->setProductCollection($collection); 

     return parent::_beforeToHtml(); 
    } 
0

我認爲,要改變New.php這樣的:

$collection = Mage::getResourceModel('catalog/product_collection');

成爲本(getStoreId可選)...

$featCat = Mage::getModel('catalog/category')->load($this->getCategory()); $collection = Mage::getResourceModel('catalog/product_collection')->setStoreId($this->getStoreId())->addCategoryFilter($featCat);

...很明顯,你必須創建getter和setter像 'setCategory', '的getCategory'

3

最後很簡單...

使用下面的定義塊:

{{block type="catalog/product_new" name="home.catalog.product.new" alias="allCatalogNewProducts" category_id="5" template="catalog/product/new.phtml"}} 

要檢索category_id,您應該修改新類的_beforeToHtml函數,如下所示:

文件在\程序\代碼\核心\法師\目錄\塊\產品\ New.php 不想收費這個文件在本地路徑

protected function _beforeToHtml() 
    { 
     $todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); 

     $collection = Mage::getResourceModel('catalog/product_collection'); 
     $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()); 

     $collection = $this->_addProductAttributesAndPrices($collection) 
      ->addStoreFilter() 
      ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate)) 
      ->addAttributeToFilter('news_to_date', array('or'=> array(
       0 => array('date' => true, 'from' => $todayDate), 
       1 => array('is' => new Zend_Db_Expr('null'))) 
      ), 'left') 
      ->addAttributeToSort('news_from_date', 'desc') 
      ->setPageSize($this->getProductsCount()) 
      ->setCurPage(1) 
     ; 

     if($categoryId=$this->getData('category_id')){ 
     $category = Mage::getModel('catalog/category')->load($categoryId); 
     $collection->addCategoryFilter($category); 
     } 


     $this->setProductCollection($collection); 

     return parent::_beforeToHtml(); 

你可以看到,一個if語句已添加:

if($categoryId=$this->getData('category_id')){ 
    $category = Mage::getModel('catalog/category')->load($categoryId); 
    $collection->addCategoryFilter($category); 
    } 

您檢索與功能$的CATEGORY_ID這個 - >的getData( 'CATEGORY_ID')

享受...