Skip to content

Commit

Permalink
Merge pull request #62 from EmicoEcommerce/fix/depersonalized-custome…
Browse files Browse the repository at this point in the history
…r-issue

fix: Get customer name and email from local storage
  • Loading branch information
bramstroker authored Apr 16, 2024
2 parents 6795908 + 01498e9 commit 89dcace
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 73 deletions.
10 changes: 3 additions & 7 deletions src/Plugin/Frontend/Magento/Framework/View/Result/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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');
Expand Down
52 changes: 0 additions & 52 deletions src/ViewModel/LoggedInCustomer.php

This file was deleted.

6 changes: 1 addition & 5 deletions src/view/frontend/layout/robinhq_customer_logged_in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="emico_robinhq_logged_in" template="Emico_RobinHq::logged_in.phtml">
<arguments>
<argument name="view_model" xsi:type="object">Emico\RobinHq\ViewModel\LoggedInCustomer</argument>
</arguments>
</block>
<block name="emico_robinhq_logged_in" template="Emico_RobinHq::logged_in.phtml"/>
</referenceContainer>
</body>
</page>
49 changes: 40 additions & 9 deletions src/view/frontend/templates/logged_in.phtml
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
<?php
/**
* @var $block \Magento\Framework\View\Element\Template
* @var $viewModel \Emico\RobinHq\ViewModel\LoggedInCustomer
*/

$viewModel = $block->getViewModel();
if (!$viewModel->shouldRender()) {
return '';
}
?>
<script type="text/javascript">
if (typeof __robin !== 'undefined') {
if (typeof __robin !== 'undefined' && isLoggedIn()) {
var customer = getCustomer();
var robin_settings = {
callback: function (event, data) {
if (event == 'init') {
var name = '<?= $viewModel->getName(); ?>'; // determine real name
var email = '<?= $viewModel->getEmail(); ?>'; // determine real email
var name = customer.fullname; // determine real name
var email = customer.email; // determine real email

if (email != null && email != '') {
var ws = __robin.getWebStore();
Expand All @@ -29,4 +24,40 @@ if (!$viewModel->shouldRender()) {
};
}

/**
* Function to check if customer is logged in
* @returns {boolean}
*/
function isLoggedIn() {
var mageCacheStorage = getMageCacheStorage();

return mageCacheStorage != null && typeof mageCacheStorage.customer !== 'undefined';
}

/**
* Get the customer from the local storage, because the PHP customer session is depersonalized on cached pages
* @returns {Object}
*/
function getCustomer() {
var mageCacheStorage = getMageCacheStorage();
if (mageCacheStorage != null && typeof mageCacheStorage.customer !== 'undefined') {
return mageCacheStorage.customer;
}

return {
fullname: 'Unknown',
email: '[email protected]'
};
}

/**
* @returns {Object|null}
*/
function getMageCacheStorage()
{
if (localStorage.getItem('mage-cache-storage') != null) {
return JSON.parse(localStorage.getItem('mage-cache-storage'));
}
return null;
}
</script>

0 comments on commit 89dcace

Please sign in to comment.