Skip to content

Magento2 success/error message not showing

I have created a custom module and added pagination in the view file after adding a message from the controller it’s not showing:

Controller file

<?php

namespace GeekayDigitalVendorsControllerIndex;

use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkMessageManagerInterface;
class Generate extends MagentoFrameworkAppActionAction
{
    protected $_customerSession;  
    protected $_validationHelper;  
    protected $_messageManager;  
    protected $request;  
    public function __construct(
        MagentoFrameworkAppActionContext $context,
        GeekayDigitalVendorsHelperValidation $validation,
        MagentoFrameworkMessageManagerInterface $messageManager,
        MagentoFrameworkAppRequestHttp $request, 
        MagentoCustomerModelSession $customerSession
    ) 
    {
        $this->request = $request;
        $this->_customerSession = $customerSession;
        $this->_validationHelper = $validation;
        $this->_messageManager  = $messageManager;
        parent::__construct($context);

    }
    public function execute()
    {
        if(!$this->_customerSession->isLoggedIn()) {
           $this->_redirect('customer/account/login');
        } 
        $orderid = $this->getRequest() ->getParam('orderid');
        $validationResult = $this->_validationHelper->frontEndValidation($orderid);
        if(isset($validationResult['status'])){
            if($validationResult['status'] == 'error'){
                $this->_messageManager->addError($validationResult['message']);
            } else {
                $this->_messageManager->addSuccess($validationResult['message']);             
            }
        }       
        return $this->_redirect($this->_redirect->getRefererUrl()); 
    }
}

Layout file:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <block class="MagentoFrameworkViewElementMessages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>
        <referenceContainer name="content">
            <block class="GeekayDigitalVendorsBlockIndexIndex" 
                name="digital_orders_list" 
                template="digital_orders.phtml" 
                cacheable="false">
            </block>
        </referenceContainer>
    </body>
</page>

Block

<?php

namespace GeekayDigitalVendorsBlockIndex;


class Index extends MagentoFrameworkViewElementTemplate 
{
    protected $customerSession;
    protected $_orderCollectionFactory;
    protected $orders;
    protected $_storeManager;
    protected $objectManager;
    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
        MagentoStoreModelStoreManagerInterface $storeManager,
        MagentoCustomerModelSession $customerSession,
        MagentoFrameworkObjectManagerInterface $objectManager
    ) 
    {
         parent::__construct($context);
        $this->_storeManager = $storeManager;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->customerSession = $customerSession;
        $this->objectManager   = $objectManager;
    }
    protected function _prepareLayout()
    {         
        parent::_prepareLayout();      
        $this->pageConfig->getTitle()->set(__('My Digital Downloads')); 
        if ($this->getOrders()) 
        {
            $pager = $this->getLayout()->createBlock('MagentoThemeBlockHtmlPager','apr.digi.pager')
                    ->setAvailableLimit(array(10=>10,20=>20,50=>50))
                    ->setShowPerPage(true)
                    ->setCollection($this->getOrders());
            $this->setChild('pager', $pager);
            $this->getOrders()->load();
        }
       return $this;
    }   
    public function getOrders()
    {

        if (!$this->orders) {            
            $page       =   ($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
            $pageSize   =   ($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 1;
            $customerId = $this->customerSession->getCustomer()->getId();
            
            /* Get Digital enabled orders only */
            $orderList = $this->_orderCollectionFactory->create()->addFieldToSelect('*')
                    ->addFieldToFilter('customer_id',$customerId)
                    ->addAttributeToFilter('status', ['in' => ['complete','processing','myfatoorah_processed']]);
            $ids = [];
            foreach ($orderList as  $_order) 
            { 
                foreach ($_order->getAllItems() as $item) 
                {
                    $product = $item->getProduct();
                    if($product->getDigitalEnable()==1){
                        array_push($ids,$_order->getId());
                    }
                }       
            } 
            $this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*')
                    ->addAttributeToFilter('entity_id', ['in' =>$ids])
                    ->setOrder('created_at','desc')
                    ->setPageSize($pageSize)
                    ->setCurPage($page);
        }
        return $this->orders;
    } 
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    public function getBaseUrl(){
        return $this->_storeManager->getStore()->getBaseUrl();
    }  
    

}