I have a phtml file which displays all subcategory with its product.I want to load the products of particular category section using ajax when user select a radio button.Give me some solution. sample code customphtml file.

<?php foreach ($categories as $category): ?>
    <li class="grid12-3">
        <a href="<?php echo $category->getUrl() ?>" class="clearfix">
            <?php if($thumbFile = $category->getThumbnail()): ?>
            <img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
            <?php endif;?>
            <span><?php echo $category->getName() ?></span></a>

    <!-- Load (3) Products from within each subcategory  -->
    $_helper = $this->helper('catalog/output');
    $products = Mage::getModel('catalog/product')
        ->addAttributeToFilter('type_id', 'simple')
    <?php $toolbar = Mage::getBlockSingleton(‘catalog/product_list’)->getToolbarBlock();
    $pager = $layout->createBlock(‘page/html_pager’);
    $toolbar->setChild(‘product_list_toolbar_pager’, $pager);
    echo $toolbar->toHtml();
    <?php  if($toolbar-<getCurrentMode()!=grid)
    <!-- Display Each product's detailed info  -->
    <?php foreach ($products as $product): ?>
        <?php // Product Image ?>
        <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" /></a>
        <?php // Product description ?>
       <input class="my-activity"
                     id="product<?php echo $_product->getId();?>"
                     name="<?php echo str_replace(" ","_",strtolower($catname['name'])) ?>"
                     value="<?php echo $_product->getId(); ?>"  <?php echo $checked; ?> onclick="saveselectedproduct(<?php echo $catname['catid']; ?>)">   <?php $_productNameStripped = $this->stripTags($product->getName(), null, true); ?>
        <h2 class="product-name"><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $productNameStripped; ?>"><?php echo $_helper->productAttribute($product, $product->getName() , 'name'); ?></a></h2>
    <?php endforeach ; ?>

      function saveselectedproduct(catid)

                 url2="<?php echo      $this->getUrl('compatibility/compatiblelist/getcollection'); ?>";
                        type: 'POST',
                        dataType: "json",
                        success: function(data) {
  • what did you tried so far? Jul 21, 2016 at 11:03
  • i had called a controller function and got the collection in ajax function but want to display the collection in that category how the collection for that category be updated
    – coder
    Jul 21, 2016 at 11:12
  • kindly add you phtml and your controller code too Jul 21, 2016 at 11:13
  • @Qaisar Satti check my updated question in which my code is added
    – coder
    Jul 22, 2016 at 4:37
  • add this js function saveselectedproduct Jul 22, 2016 at 5:03

1 Answer 1

public function getcollectionAction()
{  // you can load the collection with category id
    $category = Mage::getModel('catalog/category')->load($catid);

 $categories = $category->getAllChildren(true);
 $products = Mage::getResourceModel('catalog/product_collection');

  //if you want to display the product too
 Mage::register('category_collection', $products);
 $block = $this->getLayout()->createBlock('core/template');
    // this will json response



 foreach($collection as $product)
   // do you code here

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.