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!
- 
 first test you are able to get the $products by echo($products) or print_r($products) ??Ask Xah– Ask Xah2019年11月07日 07:37:07 +00:00Commented Nov 7, 2019 at 7:37
2 Answers 2
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!
 answered Nov 7, 2019 at 7:15
 
 
 
 Kishan Savaliya 
 
 7,8451 gold badge14 silver badges29 bronze badges
 
 - 
 @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.Kishan Savaliya– Kishan Savaliya2019年11月07日 07:34:49 +00:00Commented Nov 7, 2019 at 7:34
- 
 Thanks. Its working.MagentoDev– MagentoDev2019年11月07日 07:39:33 +00:00Commented Nov 7, 2019 at 7:39
- 
 You're most welcome. :)Kishan Savaliya– Kishan Savaliya2019年11月07日 07:39:57 +00:00Commented Nov 7, 2019 at 7:39
Just Declare
private $productPrice;
And Use $this->productPrice Instead of $productPrice
 answered Nov 7, 2019 at 7:12
 
 
 
 Waqar Ali 
 
 2,35720 silver badges47 bronze badges
 
 - 
 Thanks. Do I need to all the $productPrice to $this->productPrice?MagentoDev– MagentoDev2019年11月07日 07:16:03 +00:00Commented Nov 7, 2019 at 7:16
- 
 the one you are returningWaqar Ali– Waqar Ali2019年11月07日 07:16:55 +00:00Commented Nov 7, 2019 at 7:16
- 
 Ok. Let me try and update youMagentoDev– MagentoDev2019年11月07日 07:17:55 +00:00Commented Nov 7, 2019 at 7:17
- 
 if it worked for you accept my answerWaqar Ali– Waqar Ali2019年11月07日 07:31:21 +00:00Commented Nov 7, 2019 at 7:31
- 
 1Wellcome Happy Coding...Waqar Ali– Waqar Ali2019年11月07日 07:40:17 +00:00Commented Nov 7, 2019 at 7:40
default