1

I want to load the top menu with ajax , so far i have created another php file that will serve the menu on the header by trying to customise it to magento following this tutorial:
http://phppot.com/demo/load-dynamic-content-using-jquery-ajax/

<?php

require_once '../app/Mage.php';



Mage::app('default');
umask(0);

$_helper = Mage::helper('catalog/category');
$categories = $_helper->getStoreCategories();

?>


<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<script type="text/javascript">
function getChildrenCategories(id) {
    $('#output').html('<img src="LoaderIcon.gif" />');
    jQuery.ajax({
        url: "get_subcats.php",
        data:'id='+id,
        type: "POST",
        success:function(data){$('#output').html(data);}
    });
}


</script>

<?php 
if(!empty($categories)) {
?>

<div id="menu">
<?php 
foreach($categories as $category) { ?>

<input type="button" value="<?php echo $category["name"]; ?>" onMouseover="getChildrenCategories(<?php echo $category["id"]; ?>);" /><?php }?>  

</div>
<?php } ?>  
<div id="output"></div>


<style>
    body{width:610;}
    #menu{background: #D8F9D3;height: 40px;border-top: #F0F0F0 2px solid;}
    #menu input[type="button"]{margin-left: 2px;padding: 0px 15px;height: 40px;border: 0px;background: #F0F0F0;}

    #output{min-height:300px;border:#F0F0F0 1px solid;padding:15px;}

</style>

but i do not know exactly how to connect the get_subcats.php ajax file , to understand which parent categorie's should display the childrens.
So far i have this but i am sure it is wrong, it will not display anything.

<?php



require_once '../app/Mage.php';



Mage::app('default');
umask(0);

$_helper = Mage::helper('catalog/category');
$categories = $_helper->getStoreCategories();
if (count($categories) > 0){
    foreach($categories as $category){
        $category = Mage::getModel('catalog/category')->load($category->getId());
        $subcategories = $category->getChildrenCategories();
        if (count($subcategories) > 0){
           //  echo $category->getName();
//             echo $category->getId();      
            foreach($subcategories as $subcategory){
               //   echo $subcategory->getName();
//                  echo $subcategory->getId();
            }
        }
    }
}

if(!empty($category)) {

?>
<h3><?php echo $subcategory[0]['id'];?></h3>
<div><?php echo $subcategory[0]['name'];?><div>
<?php } ?>

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.