2

I am facing the below error in controller file, any idea what's need to be corrected, this looks like a quick fix

Missed required argument resultPageFactory in parent::__construct call. File: /home/customer/www/staging.site.com/public_html/app/code/Bg/Checkout/Controller/Cart/Index.php

 <?php
    /**
     *
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    namespace Bg\Checkout\Controller\Cart;
    
    use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
    
    class Index extends \Magento\Checkout\Controller\Cart\Index
    {
        /**
         * @var \Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;
    
        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
         * @param \Magento\Checkout\Model\Session $checkoutSession
         * @param \Magento\Store\Model\StoreManagerInterface $storeManager
         * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
         * @param \Magento\Checkout\Model\Cart $cart
         * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
         * @codeCoverageIgnore
         */
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
            \Magento\Checkout\Model\Session $checkoutSession,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
            \Magento\Checkout\Model\Cart $cart,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
        ) {
            parent::__construct(
                $context,
                $resultPageFactory,
                $scopeConfig,
                $checkoutSession,
                $storeManager,
                $formKeyValidator,
                $cart
            );
            $this->resultPageFactory = $resultPageFactory;
            $this->deleteWrongQuoteItems($cart);
        }
        
        private function deleteWrongQuoteItems() {
            $om = $this->_objectManager;
            $cart = $om->create('Magento\Checkout\Model\Cart');
            $customerSession = $om->create('Magento\Customer\Model\Session');
            if($customerSession->isLoggedIn()) {
                $group = $customerSession->getCustomer()->getGroupId();
                if($group==2 || $group==4 || $group==1) {
                    $cartQuote = $cart->getQuote();
                    $cartData = $cartQuote->getAllItems();
                    if(count($cartData)==0) {
                        $cart->truncate();
                        $cart->saveQuote();
                    }else{
                        $resource = $om->get('\Magento\Framework\App\ResourceConnection');
                        $connection = $resource->getConnection();
                        if($group==1) {
                            $sql = "DELETE qi FROM mgn_quote_item qi
                            JOIN mgn_quote q ON q.entity_id = qi.quote_id
                            JOIN mgn_catalog_category_product pc ON pc.product_id = qi.product_id
                            WHERE q.customer_group_id = 1
                            AND (pc.category_id = 5 OR pc.category_id = 6)
                            AND qi.quote_id = ?";
    
                        } else if($group==2) {
    
                            $sql = "DELETE qi FROM mgn_quote_item qi
                            JOIN mgn_quote q ON q.entity_id = qi.quote_id
                            JOIN mgn_catalog_category_product pc ON pc.product_id = qi.product_id
                            WHERE q.customer_group_id = 2
                            AND (pc.category_id = 3 OR pc.category_id = 6)
                            AND qi.quote_id = ?";
    
                        } else if($group==4) {
    
                            $sql = "DELETE qi FROM mgn_quote_item qi
                            JOIN mgn_quote q ON q.entity_id = qi.quote_id
                            JOIN mgn_catalog_category_product pc ON pc.product_id = qi.product_id
                            WHERE q.customer_group_id = 4
                            AND (pc.category_id = 3 OR pc.category_id = 5)
                            AND qi.quote_id = ?";
    
                        }
                        $result = $connection->query($sql,$cartQuote->getId());
                        if($result->rowCount()>0) {
                            $cartQuote->collectTotals()->save();
                        }
                    }
                }
            }
        }
    
        /**
         * Shopping cart display action
         *
         * @return \Magento\Framework\View\Result\Page
         */
        public function execute()
        {
            $resultPage = $this->resultPageFactory->create();
            $resultPage->getConfig()->getTitle()->set(__('Shopping Cart'));
            return $resultPage;
        }
    }
0

1 Answer 1

0

Your parent constructor arguments are fine but in wrong placements. Update parent constructor calling to this:

parent::__construct(
$context, 
$scopeConfig, 
$checkoutSession, 
$storeManager, 
$formKeyValidator, 
$cart, 
$resultPageFactory
);

This should work

2
  • $om = $this->_objectManager; Is this code can throw error ? Commented Sep 2, 2020 at 11:20
  • No, this, $this->_objectManager will work fine in your controller Commented Sep 2, 2020 at 11:33

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.