0

I want to make category like page with Layered Navigation and product list containing only those products with special price > 0. I know that you make this condition in CMS pages ( Magento 2.1.4 - List of products which have special price with widget conditions ) but I need it to be implemented via module. Here is what i achieved: enter image description here

As you can see there is layered navigation and there are products with special price only but filtering doesnt work. Here is my code:

Vendor\Module\view\frontend\layout\wyprzedaze_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <title>Wyprzedaże</title>
</head>
<body>
    <attribute name="class" value="page-with-filter"/>
    <referenceContainer name="sidebar.main">
        <block class="Magento\LayeredNavigation\Block\Navigation\Category" name="catalog.leftnav" before="-" template="layer/view.phtml">
            <block class="Magento\LayeredNavigation\Block\Navigation\State" name="catalog.navigation.state" as="state" />
            <block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalog.navigation.renderer" as="renderer" template="layer/filter.phtml"/>
        </block>
    </referenceContainer>

    <referenceContainer name="content">
        <block class="Vendor\Module\Block\Product\CustomList" name="custom.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
            <container name="category.product.list.additional" as="additional" />
            <block class="Magento\Framework\View\Element\RendererList" name="category.product.type.details.renderers" as="details.renderers">
                <block class="Magento\Framework\View\Element\Template" as="default"/>
            </block>
            <block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="category.product.addto" as="addto">
            </block>
            <block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
                <block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
            </block>
            <action method="setToolbarBlockName">
                <argument name="name" xsi:type="string">product_list_toolbar</argument>
            </action>
        </block>
    </referenceContainer>
</body>

Vendor\Module\Block\Product\CustomList.php

namespace Vendor\Module\Block\Product;

use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;

class CustomList extends ListProduct
{
    public function getLoadedProductCollection()
    {
        return $this->_productCollection;
    }

    public function setProductCollection(AbstractCollection $collection)
    {
        $this->_productCollection = $collection;
    }
}

Vendor\Module\Controller\Index\Index.php

namespace Vendor\Module\Controller\Index;

use Vendor\Module\Block\Product\CustomList;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class Index extends Action
{
    /** @var PageFactory */
    protected $pageFactory;

    /** @var  \Magento\Catalog\Model\ResourceModel\Product\Collection */
    protected $productCollection;

    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        CollectionFactory $collectionFactory
    ) {
        $this->pageFactory = $pageFactory;
        $this->productCollection = $collectionFactory->create();

        parent::__construct($context);
    }

    public function execute()
    {
        $result = $this->pageFactory->create();

        $this->productCollection
            ->addAttributeToFilter(
                'special_price',
                ['gt'=>0],
                'left'
            )->addAttributeToFilter(
                'special_from_date',
                [
                    'or' => [
                        0 => [
                            'date' => true,
                            'to' => date('Y-m-d', time()).' 23:59:59'], // todo Zend like time
                        1 => [
                            'is' => new \Zend_Db_Expr('null')
                        ],
                    ]
                ],
                'left'
            )->addAttributeToFilter(
                'special_to_date',
                [
                    'or' => [
                        0 => [
                            'date' => true,
                            'from' => date('Y-m-d', time()).' 00:00:00'],
                        1 => [
                            'is' => new \Zend_Db_Expr('null')
                        ],
                    ]
                ],
                'left'
            );


        $this->productCollection->addFieldToSelect('*');

        /** @var CustomList $list */
        $list = $result->getLayout()->getBlock('custom.products.list');
        $list->setProductCollection($this->productCollection);

        return $result;
    }
}

PS: I found many other threads with exact same question but with no usefull answer. I think this answer is the closest https://magento.stackexchange.com/a/195702/59161

if($category)
    {
      $page = $this->request->getParam('p');
      if($page == '')
      {
        $page = 1;
      }
      $this->_collection->getCurPage();
      $this->_collection->setCurPage($page);  
    }
    else
    {
      $this->_collection->setCurPage($this->getCurrentPage());
    }

But code for $this->_collection->->getCurPage() $this->_collection->setCurPage($page) Is not there and it does not work.

  • Have you apply condition on list page of special price where you want to see this ? – Sarfaraj Sipai Oct 25 '17 at 9:31
  • i've edited the "Vendor\Module\view\frontend\layout\wyprzedaze_index_index.xml" and now it is showing complete code. I am not sure what are you asking me. List page is it file Vendor\Module\Block\Product\CustomList.php ? – embed0 Oct 25 '17 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.