2

We are facing issues from the custom module which we have done for EMI calculator. The issue is when we try to load the category page from the megamenu, its throwing the below error:

1 exception(s):
Exception #0 (Exception): Notice: Undefined variable: productprice in /var/www/html/app/code/Butterfly/Emi/Block/Index/Index.php on line 52

Below is the code:

<?php


namespace Butterfly\Emi\Block\Index;

class Index extends \Magento\Framework\View\Element\Template
{

    protected $_registry;
    protected $_categoryFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        array $data = []
    )
    {
        $this->_registry = $registry;
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context, $data);
    }


    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

    public function getCategory($categoryId) 
{
    $category = $this->_categoryFactory->create();
    $category->load($categoryId);
    return $category;
}

public function getMinDownpayment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('downpayment')
   ->addAttributeToSort('downpayment', 'asc')
   ->setPage(1, 1);
   // $products->addAttributeToSelect('*');
   if($products){
    foreach($products as $productprice){
        $productprice=$productprice['downpayment'];
    }
   }else{
    $productprice=0;
   }
    return $productprice;
} 
public function getMaxDownpayment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('downpayment')
   ->addAttributeToSort('downpayment', 'desc')
   ->setPage(1, 1);
   // $products->addAttributeToSelect('*');

      if($products){
    foreach($products as $productprice){
        $productprice=$productprice['downpayment'];
    }
   }else{
    $productprice=0;
   }
    return $productprice;
} 
public function getMinInstallment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('monthlyinstallment')
   ->addAttributeToSort('monthlyinstallment', 'asc')
   //->getFirstItem()
   ->setPage(1, 1);
      if($products){
    foreach($products as $productprice){
        $productprice=$productprice['monthlyinstallment'];
    }
   }else{
    $productprice=0;
   }
    return $productprice;
} 
public function getMaxInstallment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('monthlyinstallment')
   ->addAttributeToSort('monthlyinstallment', 'desc')
   //->getFirstItem()
   ->setPage(1, 1);
      if($products){
    foreach($products as $productprice){
        $productprice=$productprice['monthlyinstallment'];
    }
   }else{
    $productprice=0;
   }
    return $productprice;
} 

public function getProductlist($categoryId,$downpayment,$monthlyinstallment) 
{

    $hireprice= $downpayment + ($monthlyinstallment*18);
    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('*')
    ->addAttributeToFilter('hireprice',array('lt'=>$hireprice))
   ->addAttributeToSort('hireprice', 'asc');
   //->setPage(1, 1);
   // $products->addAttributeToSelect('*');

    return $products;
} 

}

Any help will be appreciated!

1
  • first test you are able to get the $products by echo($products) or print_r($products) ??
    – Ask Xah
    Commented Nov 7, 2019 at 7:37

2 Answers 2

3

you can replace your code with this

<?php


namespace Butterfly\Emi\Block\Index;

class Index extends \Magento\Framework\View\Element\Template
{

    protected $_registry;
    protected $_categoryFactory;
    protected $_productPrice;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        array $data = []
    )
    {
        $this->_registry = $registry;
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context, $data);
    }


    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

    public function getCategory($categoryId) 
{
    $category = $this->_categoryFactory->create();
    $category->load($categoryId);
    return $category;
}

public function getMinDownpayment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('downpayment')
   ->addAttributeToSort('downpayment', 'asc')
   ->setPage(1, 1);
   // $products->addAttributeToSelect('*');
   if($products){
   $this->_productPrice = 0;
    foreach($products as $productprice){
        $this->_productPrice += isset($productprice['downpayment']) ? $productprice['downpayment'] : 0;
    }
   }else{
    $this->_productPrice = 0;
   }
    return $this->_productPrice;
} 
public function getMaxDownpayment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('downpayment')
   ->addAttributeToSort('downpayment', 'desc')
   ->setPage(1, 1);
   // $products->addAttributeToSelect('*');

      if($products){
      $this->_productPrice = 0;
    foreach($products as $productprice){
        $this->_productPrice += isset($productprice['downpayment']) ? $productprice['downpayment'] : 0;
    }
   }else{
    $this->_productPrice = 0;
   }
    return $this->_productPrice;
} 
public function getMinInstallment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('monthlyinstallment')
   ->addAttributeToSort('monthlyinstallment', 'asc')
   //->getFirstItem()
   ->setPage(1, 1);
      if($products){
      $this->_productPrice = 0;
    foreach($products as $productprice){
        $this->_productPrice += isset($productprice['monthlyinstallment']) ? $productprice['monthlyinstallment'] : 0;
    }
   }else{
    $this->_productPrice = 0;
   }
    return $this->_productPrice;
} 
public function getMaxInstallment($categoryId) 
{

    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('monthlyinstallment')
   ->addAttributeToSort('monthlyinstallment', 'desc')
   //->getFirstItem()
   ->setPage(1, 1);
      if($products){
        $this->_productPrice = 0;
    foreach($products as $productprice){
        $this->_productPrice += isset($productprice['monthlyinstallment']) ? $productprice['monthlyinstallment'] : 0;
    }
   }else{
    $this->_productPrice = 0;
   }
    return $this->_productPrice;
} 

public function getProductlist($categoryId,$downpayment,$monthlyinstallment) 
{

    $hireprice= $downpayment + ($monthlyinstallment*18);
    $products = $this->getCategory($categoryId)->getProductCollection();
    $products->addAttributeToSelect('*')
    ->addAttributeToFilter('hireprice',array('lt'=>$hireprice))
   ->addAttributeToSort('hireprice', 'asc');
   //->setPage(1, 1);
   // $products->addAttributeToSelect('*');

    return $products;
} 

}

Hope this will help you!

3
  • @Siva, I've updated my code in foreach loop you need to use $this->_productPrice += instead of $this->_productPrice = this. Hope this will works for you. Commented Nov 7, 2019 at 7:34
  • Thanks. Its working.
    – MagentoDev
    Commented Nov 7, 2019 at 7:39
  • You're most welcome. :) Commented Nov 7, 2019 at 7:39
3

Just Declare

private $productPrice;

And Use $this->productPrice Instead of $productPrice

7
  • Thanks. Do I need to all the $productPrice to $this->productPrice?
    – MagentoDev
    Commented Nov 7, 2019 at 7:16
  • the one you are returning
    – Waqar Ali
    Commented Nov 7, 2019 at 7:16
  • Ok. Let me try and update you
    – MagentoDev
    Commented Nov 7, 2019 at 7:17
  • if it worked for you accept my answer
    – Waqar Ali
    Commented Nov 7, 2019 at 7:31
  • 1
    Wellcome Happy Coding...
    – Waqar Ali
    Commented Nov 7, 2019 at 7:40

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.