2

I am trying to set a layered navigation filter on the custom cms page. for that, I have created a 1 cms page name test with two-column with left bar. and inside it, I have called one template file

{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::html/test.phtml"}}

in the CMS layout update, I have placed this code

<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>

for calling product filter

inside the test.phtml file I have searched the products from the collection.

if(isset($_REQUEST["test"])) {
        $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
        $imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');
        $collection = $productCollection->create()
                        ->addAttributeToSelect('*')
                        ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                        ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
                        ->addAttributeToFilter('test', $_REQUEST["test"])                
                        ->load();
    }

when I click on any of the filter options then my URL becomes

http://127.0.0.1/test?price=-1000&test=code

so can I get the filter request but it is a very tedious task to set a filter in collection manually.

And on the home page, I have set one search box for search products.

    <form id="testsearch" action="http://127.0.0.1/magento/test" >
        <div>Find Matching Part</div>
        <div>
        <input type="text" name="test">
        <button type="submit">SEARCH</button>
        </div>
    </form>

I know that this is not a standard way to do that but anyone can help me to set this filter in custom cms page.

Any help would be appreciated.

Thanks in advance..!

  • 1
    I think it's easier if we build a custom router, create custom layout and template. Using cms page is hard in this case. – Khoa TruongDinh Nov 27 '19 at 16:44
  • thank you @KhoaTruongDinh can you help me to set a product filter here.. – Bhavesh Prajapati Nov 27 '19 at 16:51
  • 1
    You get some reference from here : notabug.org/rtownley/… – Sanjay Gohil Dec 3 '19 at 6:25
1

I have use this code to resolve my issue.

   $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
   $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
   $collection = $productCollection->create()
                 ->addAttributeToSelect('*')
                 ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                 ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);

                if (@$_REQUEST["cat"]) {
                    $category = $objectManager->create('\Magento\Catalog\Model\CategoryFactory')->create()->load(@$_REQUEST["cat"]);
                    $collection->addCategoryFilter($category);
                }if (@$_REQUEST["attr_code_1"]) {
                    $collection->addFieldToFilter('attr_code_1', @$_REQUEST["attr_code_1"]);     
                }if (@$_REQUEST["attr_code_2"]) {
                    $collection->addAttributeToFilter('attr_code_2', @$_REQUEST["attr_code_2"]);
                }if (@$_REQUEST["attr_code_3"]) {
                    $collection->addAttributeToFilter('attr_code_3', @$_REQUEST["attr_code_3"]);
                }

                $collection->addAttributeToFilter('test', @$_REQUEST["@$_REQUEST["test"]"]);    
                $collection->load();

I also know that it is not the right way to do this but it works for me to resolve my issue.

If anyone have a better answer then provide here... the boundary is not closed...

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.