How to refresh the cart on the frontend after created it in the component frontend controller?

I'm want to create cart recovery functionality in my component and I'm stuck with reloading the cart on the frontend.

I'm working on standard Magento CE 2.4.2 with Luma theme and Sample Products;

I've got a frontend next CartRecovery controller: namespace Mycomponent\Myname\Controller\Events; use \Psr\Log\LoggerInterface; use \Magento\Framework\App\Action\Action; use \Magento\Framework\App\Action\Context; use \Magento\Framework\View\Result\PageFactory; use \Magento\Catalog\Model\Product; use \Magento\Catalog\Api\ProductRepositoryInterface; use \Magento\Quote\Api\CartRepositoryInterface; use \Magento\Store\Model\Store; use \Magento\Checkout\Model\Session; use \Magento\Catalog\Model\ProductFactory; use \Magento\Quote\Api\CartManagementInterface; use \Magento\Customer\Model\Session as CustomerSession; use \Magento\Framework\Exception\NoSuchEntityException; use \Mycomponent\Myname\Model\CartRecovery as CartRecoveryModel; use \Mycomponent\Myname\Helper\CookieManager; class CartRecovery extends Action { const PAGE_CART_URL = '/checkout/'; /** * @var PageFactory */ protected $resultPageFactory; /** * @var CartRepositoryInterface */ protected $cartRepositoryInterface; /** * @var Store */ protected $store; /** * @var Session */ protected $session; /** * @var ProductFactory */ protected $productFactory; /** * @var CartManagementInterface */ protected $quoteManagement; /** * @var CustomerSession */ protected $customerSession; /** * @var CartRecoveryModel */ protected $cartRecoveryModel; /** * @var ProductRepositoryInterface */ protected $productRepository; /** * @var LoggerInterface */ protected $logger; /** * @var CookieManager */ protected $cookieManagerHelper; protected $checkoutSession; /** * Cart constructor. * @param Context $context * @param PageFactory $resultPageFactory * @param ProductRepositoryInterface $productRepository * @param CartRepositoryInterface $cartRepositoryInterface * @param Store $store * @param Session $session * @param ProductFactory $productFactory * @param CartManagementInterface $quoteManagement * @param CustomerSession $customerSession */ public function __construct( Context $context, PageFactory $resultPageFactory, ProductRepositoryInterface $productRepository, CartRepositoryInterface $cartRepositoryInterface, Store $store, Session $session, ProductFactory $productFactory, CartManagementInterface $quoteManagement, CustomerSession $customerSession, CartRecoveryModel $cartRecoveryModel, LoggerInterface $logger, CookieManager $cookieManagerHelper, \Magento\Checkout\Model\Session $checkoutSession ) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; $this->cartRepositoryInterface = $cartRepositoryInterface; $this->store = $store; $this->session = $session; $this->productFactory = $productFactory; $this->quoteManagement = $quoteManagement; $this->customerSession = $customerSession; $this->cartRecoveryModel = $cartRecoveryModel; $this->logger = $logger; $this->productRepository = $productRepository; $this->cookieManagerHelper = $cookieManagerHelper; $this->checkoutSession = $checkoutSession; } /** * @return ResponseInterface|\Magento\Framework\Controller\ResultInterface|void * @throws \Magento\Framework\Exception\LocalizedException */ public function execute() { try { $query = $this->getRequest()->getParam('key'); $cartData = $this->cartRecoveryModel->recoverCartDataFromUrl($query); if (empty($cartData['sku'])) { return false; } if ($this->customerSession->isLoggedIn()) { $customer = $this->customerSession->getCustomer(); $quoteId = $this->quoteManagement->createEmptyCartForCustomer($customer->getId()); $quote = $this->cartRepositoryInterface->get($quoteId); //foreach will be replaced foreach ($cartData['sku'] as $key => $sku) { $product = $this->productRepository->get($sku); for ($i = 0; $i < $cartData['qty'][$key]; $i++) { $quote->addProduct($product); $quote->collectTotals()->save(); } } } else { $quote = $this->session->getQuote(); $quote->setStore($this->store); $quote->setCurrency(); //foreach will be replaced foreach ($cartData['sku'] as $key => $sku) { $product = $this->productRepository->get($sku); for ($i = 0; $i < $cartData['qty'][$key]; $i++) { $quote->addProduct($product); $quote->collectTotals()->save(); } } } $this->cartRepositoryInterface->save($quote); $this->checkoutSession->replaceQuote($quote)->unsLastRealOrderId(); if (!empty($this->getRequest()->getParam('client'))) { $this->cookieManagerHelper->setCookie( CookieManager::CLIENT_COOKIE, $this->getRequest()->getParam('client') ); } $this->getResponse()->setRedirect(self::PAGE_CART_URL); } catch (\Exception $e) { $this->logger->critical($e->getLogMessage()); } } } next one is Mycomponent/Myname/etc/frontend/sections.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="myname"> <section name="cart"/> </action> </config> next one is Mycomponent/Myname/etc/frontend/routes.xml <?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route frontName="myname" id="myname"> <module name="Mycomponent_Myname"/> </route> </router> </config>

The controller got a request then recreates a cart then redirects to /checkout/ page or to Magento /index. After redirect to the checkout page on the left widget with products, Magento shows me products with all prices values set to 0. After redirect to the index page cart stays empty until the customer adds the next product to the cart manually. In Mycomponent/Myname/etc/frontend/sections.xml I tried:

//for the first time: <action name="myname/events/cartrecovery"> //for the second time <action name="myname">

and in a result cart always was empty or prices in /checkout/ page aways was 0;

My question is: How can I refresh the cart in the frontend after recreating it with redirection? Or how to set/refresh prices for products on /checkout/

Read Entire Article