如何显示出售的产品类别

时间:2016-02-07 16:08:18

标签: php magento magento-1.9

我想在我的Magento 1.9网站上添加一个链接,该链接显示所有类别的所有待售产品。我发现以下文章介绍了如何通过覆盖Mage_Catalog_Block_Product_List来显示当前类别中的待售产品。

http://inchoo.net/magento/more-flexible-approach-for-listing-out-products-on-sale-in-magento/

所以这让我思考。而不是尝试创建一个新页面来显示销售的产品,而是创建一个名为Sale的类别。然后重写Mage_Catalog_Block_Product_List方法并检查当前类别是否为“销售”类别,并将$this->_productCollection设置为包含所有待售产品的集合。

我在试图让它发挥作用时遇到了一些问题。

事实上,我很难获得我的类别中显示的所有产品的集合,因此我可以将其过滤掉,以便它只包含待售的产品。目前,我的代码将$this->_productCollection设置为包含网站上所有产品的集合(包括我的可配置产品的简单产品)。我只想要在我的类别中显示的产品。

有人可以告诉我如何获取我网站上销售的所有产品以及如何设置$this->_productCollection以便显示促销产品吗?

我已粘贴到目前为止我写的代码

应用\代码\本地\公司名称\目录\块\产品\ list.php的

<?php
class CompanyName_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_List
{
    /**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    protected function _getProductCollection()
    {
        if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();
            /* @var $layer Mage_Catalog_Model_Layer */
            if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            }

            // if this is a product view page
            if (Mage::registry('product')) {
                // get collection of categories this product is associated with
                $categories = Mage::registry('product')->getCategoryCollection()
                    ->setPage(1, 1)
                    ->load();
                // if the product is associated with any category
                if ($categories->count()) {
                    // show products from this category
                    $this->setCategoryId(current($categories->getIterator()));
                }
            }

            $origCategory = null;
            if ($this->getCategoryId()) {
                $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
                if ($category->getId()) {
                    $origCategory = $layer->getCurrentCategory();
                    $layer->setCurrentCategory($category);
                    $this->addModelTags($category);
                }
            }

            // Check if the current category is the Sale category
            if ($layer->getCurrentCategory()->getId() == '14') {

                // Get all categories
                Mage::log('Root Category IDs');
                $rootCategory = Mage::getModel('catalog/category')->load('2');
                $subCategories = explode(",", $rootCategory->getAllChildren());

                // Get all products
                $products = Mage::getModel('catalog/product')->getCollection();
                $products->addAttributeToSelect('*');
                $products->joinField('category_id',
                    'catalog/category_product',
                    'category_id',
                    'product_id=entity_id',
                    null,
                    'left'
                );
                $products->addAttributeToFilter('category_id', array('in' => $subCategories));
                $products->addAttributeToFilter('status', 1);
                $products->addAttributeToFilter('visibility', 4);
                $this->_productCollection = $products;

                // Show only the products on sale
                //$this->_productCollection
                //     ->addFinalPrice()
                //     ->getSelect()
                //     ->where('price_index.final_price < price_index.price');

            } else {
                $this->_productCollection = $layer->getProductCollection();
            }

            $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

            if ($origCategory) {
                $layer->setCurrentCategory($origCategory);
            }
        }

        return $this->_productCollection;
    }
}

应用\等\模块\ CompanyName_Catalog.xml

<?xml version="1.0"?>
<config>
    <modules>
        <CompanyName_Catalog>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </CompanyName_Catalog>
    </modules>
</config>

应用\代码\本地\公司名称\目录\等\ config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <companyname_catalog>
            <version>1.0</version>
        </companyname_catalog>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product_list>CompanyName_Catalog_Block_Product_List</product_list>
                </rewrite>
            </catalog>
        </blocks>
  </global>
</config>

1 个答案:

答案 0 :(得分:0)

这会将On Sale产品的产品系列设置为当前日期:

    $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    $storeId    = Mage::app()->getStore()->getId();
    $products = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addUrlRewrite()
        ->addTaxPercents()          
        ->addStoreFilter()
        ->addAttributeToFilter('special_to_date', array('date'=>true, 'from'=> $todayDate));

    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
    $products->setPageSize($this->getConfig('qty'))->setCurPage(1);
    $this->setProductCollection($products);