【问题标题】:How to display a category of products on sale如何显示销售的产品类别
【发布时间】:2016-05-17 07:06:34
【问题描述】:

我想在我的 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方法并检查当前分类是否为Sale分类,并将$this->_productCollection设置为包含所有在售产品的集合。

我在尝试使其正常工作时遇到了一些问题。

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

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

我已经把我写的代码贴在下面了

app\code\local\CompanyName\Catalog\Block\Product\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;
    }
}

app\etc\modules\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>

app\code\local\CompanyName\Catalog\etc\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>

【问题讨论】:

    标签: php magento magento-1.9


    【解决方案1】:

    这会将 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      相关资源
      最近更新 更多