1

i can't find a solution for my problem: i have a category with a layout.phtml created by me. in this category i want to show only the new products (setted up by new from date and new to date fields) of this category. i don't understand how to do this (xml or php no matter). anyone can help me?

4

2 回答 2

1

请尝试此操作以按类别 ID 过滤所有新产品 -

public function getProductCollection($catIds) {
$productIds = $this->getProductIdsByCategories($catIds);
$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    $storeId    = Mage::app()->getStore()->getId();
    $collection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
                        ->setStoreId($storeId)
                        ->addStoreFilter($storeId)
                        ->addMinimalPrice()
                        ->addTaxPercents()
                        ;
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $collection
        ->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')

        ;

    if(count($productIds)) {
        $collection->addFieldToFilter('entity_id', array('in'=>$productIds));
    }

    return $collection;
}

希望它会有所帮助。

谢谢!

于 2012-08-21T09:43:21.977 回答
0

不起作用,但我已经以这种方式解决了(希望它可以帮助某人):

  • 首先,进入 CMS -> WIDGETS,然后创建 Catalog New Products List 的小部件。在里面,进行新的布局更新:显示在锚类别、主要内容、产品网格上。

  • 其次,转到 template/catalog/product/widget/new/content/new_grid.phtml,然后添加以下内容:

            $currentCategory = Mage::registry('current_category');
            $category_model = Mage::getModel('catalog/category');
            $all_child_categories = $category_model->getResource()->getAllChildren($currentCategory);
    
  • 第三,在 foreach ($_products->getItems() as $_product) 之后添加:

        $visualizzo="";
    
            $diff = array_diff($all_child_categories, $_product->getCategoryIds());
            if(count($diff)==count($all_child_categories)){
                $visualizzo="display:none;";
            }
    
  • 最后,在 li class="item" 有一个样式,把它放进去:

于 2012-08-21T10:27:05.537 回答