3

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>
    </li>

    <!-- Load (3) Products from within each subcategory  -->
    <?php
    $_helper = $this->helper('catalog/output');
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addCategoryFilter($category)
        ->addAttributeToFilter('type_id', 'simple')
         ->addAttributeToSelect('*');
        ->load();
    ?>
    <?php $toolbar = Mage::getBlockSingleton(‘catalog/product_list’)->getToolbarBlock();
    $toolbar->setCollection($products);
    $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): ?>
        <li>
        <?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"
                     type="radio"
                     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>
        </li>
    <?php endforeach ; ?>

      function saveselectedproduct(catid)
      {

                 url2="<?php echo      $this->getUrl('compatibility/compatiblelist/getcollection'); ?>";
              $j.ajax({
                        url:url2,
                        type: 'POST',
                        dataType: "json",
                        data:{catid:catid},
                        success: function(data) {
                               alert($data);
                        }});
            }
  </script>
12
  • 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

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

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

  //if you want to display the product too
 $this->loadLayout(); 
 Mage::register('category_collection', $products);
 $block = $this->getLayout()->createBlock('core/template');
    $block->setTemplate('test/test.phtml');
    // this will json response
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($block->toHtml())); 

}

test.phtml

$collection=Mage::registry('category_collection');
 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.