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!