7

Magento2 version is - 2.1.2

I am creating custom module for layered navigation and i want to create multiselect options functionality for layerednavigation.

I have already displayed checkbox over there by overriding the template of layered navigation in my custom module now what i need is how to select those checkbox as multiselect**.

I have seen there are lot of extenstions available for that but i want to do it with code.is it possible ?

Is there any good tutorial to override layered navigation module and create this kind of functionality ?

Advance Thanks.

2
  • is your ajax filter working ?? only multi select is not working only ?? @ManthanDave Nov 25 '16 at 11:54
  • @MohammadMujassam - Firstly i have started multi select option i dont mind if its work with post on page referesh , if its work then i will implement ajax at last Nov 25 '16 at 12:09
0

First of all you need to create you own/custom theme.. inside your theme directory create folder Magento_LayeredNavigation (if not available already) inside this folder copy the all template file from core module Magento\LayeredNavigation\view\frontend\templates into Magento_LayeredNavigation\template.

now you can override the layered navigation design and codes..

for multi-select you need to write the url, Sample url re-write method given as below you can customize it as per you requirement.

sample helper function given below..

public function getFilterUrls($params,$filter,$value){
    $currentCatUrl = $this->getCurrentCatUrl();
    unset($params['id']);
    unset($params['isAjax']);
    unset($params['_']);
    //$value = str_replace(',', '', $value);
    if (isset($params[$filter])) {
        $filterArray = explode(',', $params[$filter]);
        $checkValue = in_array($value, $filterArray);
        $remVelue = array_diff($filterArray, [$value]);
        $remVelue = implode(',', $remVelue);
    }
    if(isset($params[$filter]) and $params[$filter] == $value){
        unset($params[$filter]);
        $url = $currentCatUrl . '?' . http_build_query($params);
    }elseif (!empty($params[$filter]) && $params[$filter] !== ',' && $params[$filter] !== $value && $checkValue == FALSE) {
        $urlValues = $params[$filter];
        unset($params[$filter]);
        $url = $currentCatUrl . '?' . http_build_query($params) . '&' . $filter . '='.$urlValues.','.$value;
    }elseif (isset($params[$filter]) && $params[$filter] !== ',' && $params[$filter] !== $value && $checkValue == TRUE) {
        unset($params[$filter]);
        $url = $currentCatUrl . '?' . http_build_query($params) . '&' . $filter . '='.$remVelue;
    }
    else{
        unset($params[$filter]);
        $url = $currentCatUrl . '?' . http_build_query($params) . '&' . $filter . '=' . $value ;
    }
    return $url;
}

where $params = $this->getRequest()->getParams();

AND $filter is new/ old parameter name which you want make multiselect/select.

AND $value is the value you want for the parameter you passed..

AND $currentCatUrl current category url which you will get from registory.

call the above helper method in Magento_LayeredNavigation\template\view.phtml. Magento_LayeredNavigation\template\view.phtml is the file where you make the changes for layered navigation..

this will return the filter URL for both multiselect & sigle select.

update 1: formate example

<a href="<?php echo $block->escapeUrl($helper->getFilterUrls($params,'for','1')) ?>">
                <input type="checkbox" name="" value="l" id="check1"  <?php echo $this->getRequest()->getParam('for')=='1'?'checked':'' ?>>
                <label for="check1"> </label> <div class="anchor-text">Men</div> 

5
  • please check the method I have given all the filters which is already there will not be remove .. if you are clicking on the same checkbox which is already applied then only it will remove Nov 25 '16 at 13:25
  • please check my update 1 Nov 25 '16 at 13:28
  • create helper in any of your module and load into the view.phtml (template) as given $helper = $this->helper('Vendor\Module\Helper\Data') you can refer this site to create the helper cloudways.com/blog/create-helpers-in-magento-2 Nov 25 '16 at 13:32
  • May be you are missing some param or some thing you can debug it.. basically the logic will be same as i have given... some params may changes as per your requirement... and yes you can use this anywhere Nov 25 '16 at 14:03
  • okay i have done all the things and now page is blank when i am trying to access the page. is there any other issue ? - <?php $helper = $this->helper('vendorname\LayeredNavigation\Helper\Data'); ?> <?php $params = $this->getRequest()->getParams(); ?> <a href="<?php echo $block->escapeUrl($helper->getFilterUrls($params,'for','1')) ?>"></a> - i have puted this line on my view.phtml top Nov 26 '16 at 5:13

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.