From b2794b8763624236f432472b7a92c6e800ef5668 Mon Sep 17 00:00:00 2001 From: tjeujansen Date: Tue, 16 Apr 2024 11:38:43 +0200 Subject: [PATCH 1/2] fix: Get customer name and email from local storage --- src/ViewModel/LoggedInCustomer.php | 24 --------------------- src/view/frontend/templates/logged_in.phtml | 24 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/ViewModel/LoggedInCustomer.php b/src/ViewModel/LoggedInCustomer.php index 9ea44ee..6a44730 100644 --- a/src/ViewModel/LoggedInCustomer.php +++ b/src/ViewModel/LoggedInCustomer.php @@ -7,16 +7,13 @@ use Magento\Customer\Model\Session; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\View\Element\Block\ArgumentInterface; -use Psr\Log\LoggerInterface; class LoggedInCustomer implements ArgumentInterface { /** - * @param LoggerInterface $logger * @param Session $customerSession */ public function __construct( - private LoggerInterface $logger, private Session $customerSession ) { } @@ -28,25 +25,4 @@ public function shouldRender(): bool { return $this->customerSession->getCustomerId() !== null; } - - /** - * @return string - */ - public function getName(): string - { - try { - return $this->customerSession->getCustomer()->getName() ?? 'Unknown'; - } catch (LocalizedException $e) { - $this->logger->error('Could not get customer name', ['exception' => $e]); - return 'Unknown'; - } - } - - /** - * @return string - */ - public function getEmail(): string - { - return $this->customerSession->getCustomer()->getEmail(); - } } diff --git a/src/view/frontend/templates/logged_in.phtml b/src/view/frontend/templates/logged_in.phtml index 47cbce8..5260908 100644 --- a/src/view/frontend/templates/logged_in.phtml +++ b/src/view/frontend/templates/logged_in.phtml @@ -11,11 +11,12 @@ if (!$viewModel->shouldRender()) { ?> From 01498e975d7df3833c5e08a7914eaaf1c5be965d Mon Sep 17 00:00:00 2001 From: tjeujansen Date: Tue, 16 Apr 2024 12:52:16 +0200 Subject: [PATCH 2/2] fix: Get all session data from local storage --- .../Magento/Framework/View/Result/Layout.php | 10 ++--- src/ViewModel/LoggedInCustomer.php | 28 ------------ .../layout/robinhq_customer_logged_in.xml | 6 +-- src/view/frontend/templates/logged_in.phtml | 45 ++++++++++++------- 4 files changed, 32 insertions(+), 57 deletions(-) delete mode 100644 src/ViewModel/LoggedInCustomer.php diff --git a/src/Plugin/Frontend/Magento/Framework/View/Result/Layout.php b/src/Plugin/Frontend/Magento/Framework/View/Result/Layout.php index 9544bd4..30e4172 100644 --- a/src/Plugin/Frontend/Magento/Framework/View/Result/Layout.php +++ b/src/Plugin/Frontend/Magento/Framework/View/Result/Layout.php @@ -5,16 +5,14 @@ namespace Emico\RobinHq\Plugin\Frontend\Magento\Framework\View\Result; use Magento\Checkout\Helper\Cart; -use Magento\Customer\Model\Session; class Layout { /** * Layout constructor. - * @param Session $customerSession - * @param Cart $cartHelper + * @param Cart $cartHelper */ - public function __construct(private Session $customerSession, private Cart $cartHelper) + public function __construct(private Cart $cartHelper) { } @@ -31,9 +29,7 @@ public function afterAddDefaultHandle( \Magento\Framework\View\Result\Layout $subject, mixed $result ) { - if ($this->customerSession->getCustomerId()) { - $result->addHandle('robinhq_customer_logged_in'); - } + $result->addHandle('robinhq_customer_logged_in'); if ($this->cartHelper->getItemsCount() !== 0) { $result->addHandle('robinhq_cart_contents'); diff --git a/src/ViewModel/LoggedInCustomer.php b/src/ViewModel/LoggedInCustomer.php deleted file mode 100644 index 6a44730..0000000 --- a/src/ViewModel/LoggedInCustomer.php +++ /dev/null @@ -1,28 +0,0 @@ -customerSession->getCustomerId() !== null; - } -} diff --git a/src/view/frontend/layout/robinhq_customer_logged_in.xml b/src/view/frontend/layout/robinhq_customer_logged_in.xml index 7916683..358b5fd 100644 --- a/src/view/frontend/layout/robinhq_customer_logged_in.xml +++ b/src/view/frontend/layout/robinhq_customer_logged_in.xml @@ -3,11 +3,7 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> - - - Emico\RobinHq\ViewModel\LoggedInCustomer - - + diff --git a/src/view/frontend/templates/logged_in.phtml b/src/view/frontend/templates/logged_in.phtml index 5260908..689b378 100644 --- a/src/view/frontend/templates/logged_in.phtml +++ b/src/view/frontend/templates/logged_in.phtml @@ -1,16 +1,10 @@ getViewModel(); -if (!$viewModel->shouldRender()) { - return ''; -} ?>