0

when user comes first time in a site it will ask for category subcategory etc..after that i store all value into session then if user come to home page it will show it's working fine for all except subcategory

  <?php $tree = $this->helper('techinflo_shop')->getCategoriesDropdown(); ?>
 <?php 
      $categoryvalue = Mage::getSingleton('core/session')->getCategoryValue();
      $attributeValue = Mage::getSingleton('core/session')->getAttributeValue();
      $homecityvalue = Mage::getSingleton('core/session')->getHomeCityValue();
      $subcategoryvalue = Mage::getSingleton('core/session')->getSubCategoryValue();
    ?> 
<script type="text/javascript">
    var children = $H(<?php echo json_encode($tree['children']) ?>);
    function showCat(obj, level) {
        hideContent();
        var catId = obj.value;
        level += 1;
        if ($('cat_container_' + level)) {
            $('cat_container_' + level).remove();
        }
        if (children.get(catId)) {
            var options = children.get(catId);
            //var html = '<select class="form-control" name="subcategory" required="required" id="cat_' + catId + '" onchange="showCat(this, ' + level + ')">';
            var html = '<select class="form-control" name="subcategory" required="required" id="cat_' + catId + '" onchange="showCat()">';
            //html += '<option value=""disabled selected> Select </option>';

            html += '<option value="' + options[i].entity_id + '"' + (options[i].entity_id == '<?php echo $subcategoryvalue?>') ? ' selected="selected"' : '' + '>' + options[i].name + '</option>';  


            for (var i = 0; i < options.length; i++) {
                html += '<option value="' + options[i].entity_id + '">' + options[i].name + '</option>';
            }
            html += '</select>';
            html = '<div id="cat_container_' + level + '">' + html + '</div>';

            $('sub_cat').insert(html);
        }
    }
    //window.onload=showContent();  
     /*function showContent(){
        document.getElementById("default-content").style.display="block";
    }
    function hideContent(){
        document.getElementById("default-content").style.display="none";
    }*/
    function onLoad()
{
    showContent();
    showCat(document.getElementById('first_cat'), 2);
}

window.onload=onLoad();
</script>

<form role="form" method="post" action="<?php echo Mage::getUrl('location/index'); ?>" onsubmit="return validation();">
    <div class="search_content_area">
        <div class="i">
            <span> I am a </span>
        </div>
        <div class="category">
            <select id="first_cat" onchange="showCat(this, 2)" class="form-control" required="required" name="parentcate">
                <option value="" disabled selected>Select</option>
                <?php foreach ($tree['first'] as $cat): ?>
                    <option value="<?php echo $cat->getUrlKey() ?>" <?php echo ($categoryvalue==$cat->getId())?'selected':'' ?>><?php echo $cat->getName() ?></option>
                <?php endforeach ?>
            </select>
        </div>
        <div class="city">
            <span> Shopping City </span>
        </div>
        <div class="select-city">
            <select class="form-control" id="mytext1" required="required" name="homecity">
                <option value="" disabled selected>Select</option>
                <option value="Bangalore" <?php echo ($homecityvalue=='Bangalore')?'selected':'' ?>>Bangalore</option>
                <option value="Calcutta" <?php echo ($homecityvalue=='Calcutta')?'selected':'' ?>>Calcutta</option>
                <option value="Chennai" <?php echo ($homecityvalue=='Chennai')?'selected':'' ?>>Chennai</option>
            </select>
        </div>
        <div class="look">
            <span> Looking For </span>
        </div>
        <div id="sub_cat"></div>
        <div id="default-content">
        <div id="cat_container_va">
          <select class="form-control" id="mytext">
            <option disabled selected>Select</option>
          </select> 
          </div>
        </div>
        <div class="by">
            <span> By </span>
        </div>
        <div class="brand">          
            <select class="attribute-brand form-control" id="attribute" required="required" name="attribute"> 
                  <option>Select</option>
                <?php 
                $attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
                $attributeArray=array();
                foreach($attributes as $a){
                  if($a->getAttributeCode() == 'brand'){
                    foreach($a->getSource()->getAllOptions(false) as $option){?>
                      <option value="<?php echo $option['value'];?>" <?php echo ($attributeValue==$option['value'])?'selected':'' ?>><?php echo $option['label'];?></option>
                    <?php }
                  }
                }

                ?>
            </select> 
          </div>
        <button class="btn btn-success btn-block let-shop" type="submit">Go</button>
    </div>


    <div style="clear:both"></div>
</form>

1 Answer 1

0

I see that when you build the subcategory you do not set a selected value.

html += '<option value="' + options[i].entity_id + '">' + options[i].name + '</option>';  

Try changing it to this:

html += '<option value="' + options[i].entity_id + '"' + (options[i].entity_id == '<?php echo $subcategoryvalue?>') ? ' selected="selected"' : '' + '>' + options[i].name + '</option>';  

I haven't tested the code, so be careful at possible syntax error.

[EDIT]

I see that the subcategories dropdown appears after you call the function showCat on changing the category dropdown.
When the page loads this does not happen.
So you can try to add this code inside your script tag:

function onLoad()
{
    showContent();
    showCat(document.getElementById('first_cat'), 2);
}

And replace the line window.onload=showContent(); with window.onload=onLoad();

9
  • After change sub category dropdown also not showing
    – Magento 2
    Commented Aug 4, 2016 at 12:50
  • ahh..it is not showing. I thought it is showing but not selecting the correct value
    – Marius
    Commented Aug 4, 2016 at 12:51
  • No luck check my updated question of full code
    – Magento 2
    Commented Aug 4, 2016 at 13:00
  • You commented the code for showContent. Put it back. same for hideContent
    – Marius
    Commented Aug 4, 2016 at 13:31
  • Sorry i am not getting i need to uncoment hidecontent or what
    – Magento 2
    Commented Aug 4, 2016 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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