【问题标题】:Change Magento New Product New.php to be based off id rather than to & from date将 Magento 新产品 New.php 更改为基于 id 而不是 to & from date
【发布时间】:2012-11-23 09:36:10
【问题描述】:

我想改变 Magento 核心文件 New.php 将新产品调用成块的方式。

该文件位于 app/code/core/Mage/Catalog/Block/Product/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('or'=> array(
                0 => array('date' => true, 'to' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter('news_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToFilter(
                array(
                    array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
                    array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
                    )
              )
            ->addAttributeToSort('news_from_date', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1)
        ;

        $this->setProductCollection($collection);

        return parent::_beforeToHtml();
    }

该文件通过从产品管理员中提取 New Product FromNew Product To 日期字段的可选字段来工作。考虑到我的目录大小和更新这些字段所需的手动管理,这很不方便。所以基本上我想更改功能以获取 MAX 产品 ID(即添加的最新产品)并在此之前运行到 100。这将列出商店中最近的 100 种产品。

我尝试过类似的方法,但没有成功。

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter ('entity_id')
            ->addAttributeToSort('entity_id', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1)
        ;

        $this->setProductCollection($collection);

        return parent::_beforeToHtml();
    }

这只是试图根据产品 ID (entity_id) 返回产品,但没有返回任何东西(它也没有给出任何 php 错误)。

【问题讨论】:

    标签: php magento magento-1.5


    【解决方案1】:

    尝试(删除 ->addAttributeToFilter ('entity_id')... 在 v1.7 上测试)

    ....
    $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToSort('entity_id', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1);
    ...
    

    【讨论】:

    • 是的,这有效!谢谢。它总是最简单的事情。我遇到了一些问题,它只返回 5 个产品,但我正在通过一些 XML 更改对其进行排序。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    相关资源
    最近更新 更多