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
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..!
-
1I think it's easier if we build a custom router, create custom layout and template. Using cms page is hard in this case.Khoa Truong– Khoa Truong2019年11月27日 16:44:15 +00:00Commented Nov 27, 2019 at 16:44
-
thank you @KhoaTruongDinh can you help me to set a product filter here..Bhavesh Prajapati– Bhavesh Prajapati2019年11月27日 16:51:33 +00:00Commented Nov 27, 2019 at 16:51
-
1You get some reference from here : notabug.org/rtownley/…Sanjay Gohil– Sanjay Gohil2019年12月03日 06:25:47 +00:00Commented Dec 3, 2019 at 6:25
2 Answers 2
Go with all below mentioned solutions/post, some of them will definitely help you.
Layered navigation for custom collection on custom page - magento2
https://webkul.com/blog/how-to-create-layered-navigation-on-custom-page-in-magento-2/
https://www.mageplaza.com/blog/custom-product-collection-layered-navigation-magento-2.html
http://www.techytalk.info/adding-layered-navigation-custom-controller-action-magento/
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...
Explore related questions
See similar questions with these tags.