1

I have problem in my controller

when not login my controller is run work

enter image description here

but when login with custom customer group (VIP member) my controller is not work and redirect to main page (index)

this is my code

    <?php
/**
*
* Copyright © 2015 commerce. All rights reserved.
*/
namespace Moneyok\Custommoneyok\Controller\Index;
class SortByCat extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $_cacheTypeList;
/**
* @var \Magento\Framework\App\Cache\StateInterface
*/
protected $_cacheState;
/**
* @var \Magento\Framework\App\Cache\Frontend\Pool
*/
protected $_cacheFrontendPool;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param Action\Context $context
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\App\Cache\StateInterface $cacheState
* @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\App\Cache\StateInterface $cacheState,
\Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheState = $cacheState;
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->resultPageFactory = $resultPageFactory;
}

/**
* Flush cache storage
*
*/
public function execute()
{
$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('TT\Hello\Block\HelloWorld');
$currentCategory = $myBlock->getCurrentCategory();
$params = $this->getRequest()->getParams();
// echo "55555;";
$s = $params['s'];
$sortby = $params['sortby'];
$fill = $params['fill'];
$idja = $params['id'];


$productCollection = $myBlock->getProductCollection();

$productCollection->addCategoriesFilter(['eq'=>$idja]);
$productCollection->setPageSize($s);
$productCollection->setOrder($sortby,$fill);
foreach ($productCollection as $product) {
$productName=$product->getName();
$productId=$product->getId();
$productImg=$product->getImage();
$productUrl=$product->getProductUrl();
$productPrice=$product->getPrice();
$productSku=$product->getSku();
$price= number_format((float)$productPrice, '2', '.', ',');
$tier_price = $product->getTierPrice();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$RatingOb = $objectManager->create('Magento\Review\Model\Rating')->getEntitySummary($productId);
@$ratings = $RatingOb->getSum()/$RatingOb->getCount();
if (!$ratings=="") {
$ratings;
}else{
@$ratings=0;
}
$json_data[]=array("productId"=>$productId,"productName"=>$productName,"productImg"=>$productImg,"productUrl"=>$productUrl,"productSku"=>$productSku,"productPrice"=>$price);
//"productTierPrice"=>$tier_price,,"ratings"=>@$ratings
}
$json=json_encode($json_data,JSON_UNESCAPED_UNICODE);
echo $json;

}
}

error log

a:4:{i:0;s:127:"Notice: Undefined variable: json_data in /var/www/html/app/code/Moneyok/Custommoneyok/Controller/Index/SortByCat.php on line 87";i:1;s:8901:"#0 /var/www/html/app/code/Moneyok/Custommoneyok/Controller/Index/SortByCat.php(87): Magento\Framework\App\ErrorHandler->handler(8, 'Undefined varia...', '/var/www/html/a...', 87, Array)
#1 /var/www/html/var/generation/Moneyok/Custommoneyok/Controller/Index/SortByCat/Interceptor.php(24): Moneyok\Custommoneyok\Controller\Index\SortByCat->execute()
#2 /var/www/html/vendor/magento/framework/App/Action/Action.php(102): Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor->execute()
#3 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#4 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(70): Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor->___callParent('dispatch', Array)
#5 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Moneyok\\Customm...', 'dispatch', Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Array, 'customer-app-ac...')
#6 /var/www/html/vendor/magento/module-customer/Model/App/Action/ContextPlugin.php(61): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#7 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Customer\Model\App\Action\ContextPlugin->aroundDispatch(Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#8 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Moneyok\\Customm...', 'dispatch', Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Array, 'contextPlugin')
#9 /var/www/html/vendor/magento/module-store/App/Action/Plugin/Context.php(106): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#10 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Store\App\Action\Plugin\Context->aroundDispatch(Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#11 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Moneyok\\Customm...', 'dispatch', Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Array, 'storeCheck')
#12 /var/www/html/vendor/magento/module-store/App/Action/Plugin/StoreCheck.php(44): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#13 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Store\App\Action\Plugin\StoreCheck->aroundDispatch(Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#14 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Moneyok\\Customm...', 'dispatch', Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Array, 'tax-app-action-...')
#15 /var/www/html/vendor/magento/module-tax/Model/App/Action/ContextPlugin.php(91): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#16 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Tax\Model\App\Action\ContextPlugin->aroundDispatch(Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#17 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Moneyok\\Customm...', 'dispatch', Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Array, 'weee-app-action...')
#18 /var/www/html/vendor/magento/module-weee/Model/App/Action/ContextPlugin.php(112): Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#19 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\Weee\Model\App\Action\ContextPlugin->aroundDispatch(Object(Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#20 /var/www/html/var/generation/Moneyok/Custommoneyok/Controller/Index/SortByCat/Interceptor.php(39): Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor->___callPlugins('dispatch', Array, Array)
#21 /var/www/html/vendor/magento/framework/App/FrontController.php(55): Moneyok\Custommoneyok\Controller\Index\SortByCat\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#22 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#23 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#24 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'requestPreproce...')
#25 /var/www/html/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#26 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#27 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#28 /var/www/html/vendor/magento/framework/Module/Plugin/DbStatusValidator.php(69): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#29 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#30 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#31 /var/www/html/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(73): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Request\Http))
#32 /var/www/html/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#33 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'front-controlle...')
#34 /var/www/html/vendor/magento/module-page-cache/Model/App/FrontController/VarnishPlugin.php(55): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#35 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(142): Magento\PageCache\Model\App\FrontController\VarnishPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#36 /var/www/html/var/generation/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#37 /var/www/html/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#38 /var/www/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#39 /var/www/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#40 {main}";s:3:"url";s:24:"/moneyok/index/SortByCat";s:11:"script_name";s:10:"/index.php";}
  • can you please add error log – Vaibhav Ahalpara Dec 23 '16 at 5:21
  • @vaibhavahalpara update error – tttk Dec 23 '16 at 6:11
  • Notice: Undefined variable: json_data I think change $json_data[] to $json_data so a notice will remove then we see what is issued – Vaibhav Ahalpara Dec 23 '16 at 6:16
  • @vaibhavahalpara but i not login or login with cutomer_group=cutomer my controller is work but i login user customer_group=vip member my controller is not work when to call have redirect to index – tttk Dec 23 '16 at 6:16
  • some time SortByCat this type of action not working can you can SortByCat to just Sortbycat – Vaibhav Ahalpara Dec 23 '16 at 6:17
0

Finally i found the bug by myself by following the below code.

// $tier_price = $product->getTierPrice();
// $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// $RatingOb = $objectManager->create('Magento\Review\Model\Rating')->getEntitySummary($productId);
// @$ratings = $RatingOb->getSum()/$RatingOb->getCount();
// if (!$ratings=="") {
// $ratings;
// }else{
// @$ratings=0;
// }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.