0

How to show Minimum price and Maximum price of this category product. In Below code i displayed category name, image and url. I want to show Minimum price and Maximum price of this category product

$layer = Mage::getSingleton('catalog/layer'); $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); if ($category->getId()) { $origCategory = $layer->getCurrentCategory(); $layer->setCurrentCategory($category); } $collection = $layer->getProductCollection(); $collection->getSelect()->reset('order'); $collection->getSelect()->order('minimal_price','asc'); $collection->getSelect()->limit(1); 

    <div class="container">

        <div class="row thmub_list">
            <?php
            $catIds = explode(',', $this->getIds());

              $categories = array();
            foreach ($catIds as $catId) {
                $category = Mage::getModel('catalog/category')->load($catId);
                $categories[$category->getName()] = array(
                    'url' => $category->getUrl(),
                    'img' => $category->getImageUrl()
                );
            }

            ksort($categories, SORT_STRING);
            ?>        
                <?php if ($category->getIsActive()): ?>
                    <?php foreach ($categories as $name => $data): ?>
                  <div class="col-cxs-12 col-xs-12 col-xs-6 col-sm-6 col-md-4 col-lg-4 custom-item">
                        <div class="panel">          
                            <a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>">
                                <img id="cat-image" src="<?php echo $data['img']; ?>" class="img-responsive"/></a>
                              <h3 class="product-title"><a href="<?php echo $data['url']; ?>"><?php echo $name; ?></a></h3>
                            <button type="button" title="<?php echo $this->__('SELECT BOUQUETS') ?>" class="btn btn-default custom-btn" onclick="setLocation('<?php echo $data['url']; ?>')"><?php echo $this->__('SELECT BOUQUETS') ?></button>     
<?php echo $collection->getFirstItem()->getMinimalPrice();?>                
                        </div>
                  </div>
                    <?php endforeach; ?>
                <?php endif; ?>        
        </div>
    </div>

I want to Show Min Price - Max Price in after category name

1 Answer 1

2

Try this way:

$layer = Mage::getSingleton('catalog/layer'); $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); if ($category->getId()) { $origCategory = $layer->getCurrentCategory(); $layer->setCurrentCategory($category); } $collection = $layer->getProductCollection(); $collection->getSelect()->reset('order'); $collection->getSelect()->order('minimal_price','asc'); $collection->getSelect()->limit(1); 

    <div class="container">

        <div class="row thmub_list">
            <?php
            $catIds = explode(',', $this->getIds());

              $categories = array();
            foreach ($catIds as $catId) {
                $category = Mage::getModel('catalog/category')->load($catId);

    /* new code started */
    //get products from category ID
    $products = $category //Mage::getModel('catalog/category')->load($catId)
     ->getProductCollection()
     ->addAttributeToSelect('*') // add all attributes - optional
     ->addAttributeToFilter('status', 1) // enabled
     ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
     ->setOrder('price', 'ASC'); //sets the order by price

   $largeNumber = 9999999999;
   $minPrice = $largeNumber; //set to higher number 
   $maxPrice = 0;
   foreach($products as $product){
     if($product->getFinalPrice() < $minPrice){
         $minPrice = $product->getFinalPrice();
     }
     if($product->getFinalPrice() > $maxPrice){
         $maxPrice = $product->getFinalPrice();
     }
   }

   if($minPrice == $largeNumber){
       $minPrice = 0;
   }

   //echo "<div>".$minPrice."</div>";
  // echo "<div>".$maxPrice."</div>";

   /* new code finished */

                $categories[$category->getName()] = array(
                    'url' => $category->getUrl(),
                    'img' => $category->getImageUrl(),
                    'minPrice' => $minPrice,
                    'maxPrice' => $maxPrice
                );
            }

            ksort($categories, SORT_STRING);
            ?>        
                <?php if ($category->getIsActive()): ?>
                    <?php foreach ($categories as $name => $data): ?>
                  <div class="col-cxs-12 col-xs-12 col-xs-6 col-sm-6 col-md-4 col-lg-4 custom-item">
                        <div class="panel">          
                            <a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>">
                                <img id="cat-image" src="<?php echo $data['img']; ?>" class="img-responsive"/></a>
                              <h3 class="product-title"><a href="<?php echo $data['url']; ?>"><?php echo $name; ?></a></h3>
                              <div><?php echo $data['minPrice'];?></div>
                              <div><?php echo $data['maxPrice'];?></div>
                            <button type="button" title="<?php echo $this->__('SELECT BOUQUETS') ?>" class="btn btn-default custom-btn" onclick="setLocation('<?php echo $data['url']; ?>')"><?php echo $this->__('SELECT BOUQUETS') ?></button>     
<?php echo $collection->getFirstItem()->getMinimalPrice();?>                
                        </div>
                  </div>
                    <?php endforeach; ?>
                <?php endif; ?>        
        </div>
    </div>

Hope this works.

6
  • it shows only one category not all. i need to dispaly next to category name in each category
    – Magento 2
    Commented Jun 6, 2016 at 4:46
  • That means the code should be inside foreach ($catIds as $catId) { loop. Or your another loop at bottom. Which one is it you are using to render? Could you give me url if you have? Commented Jun 6, 2016 at 4:48
  • Okay i will check and let you know
    – Magento 2
    Commented Jun 6, 2016 at 4:53
  • Check my updated answer. Commented Jun 6, 2016 at 4:53
  • how to display 2 zero's after that decimals
    – Magento 2
    Commented Jun 6, 2016 at 11:08

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.