Skip to content

Commit 2431493

Browse files
author
Anna Bukatar
committed
ACP2E-2092: Minicart is not updating in stores with subdomains, even though the session is shared.
1 parent 7f58418 commit 2431493

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Test\Unit\ViewModel;
9+
10+
use PHPUnit\Framework\TestCase;
11+
12+
class CookieSettings extends TestCase
13+
{
14+
/**
15+
* @var \Magento\Customer\ViewModel\CookieSettings
16+
*/
17+
private $cookieSettings;
18+
19+
/**
20+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit\Framework\MockObject\MockObject
21+
*/
22+
private $scopeConfigMock;
23+
24+
protected function setUp(): void
25+
{
26+
$this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
27+
->getMockForAbstractClass();
28+
29+
$this->cookieSettings = new \Magento\Customer\ViewModel\CookieSettings(
30+
$this->scopeConfigMock
31+
);
32+
}
33+
34+
public function testGetCookieDomain()
35+
{
36+
$this->scopeConfigMock->expects($this->once())
37+
->method('getValue')
38+
->with(
39+
\Magento\Customer\ViewModel\CookieSettings::XML_PATH_COOKIE_DOMAIN,
40+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
41+
)
42+
->willReturn('example.com');
43+
44+
$this->assertEquals('example.com', $this->cookieSettings->getCookieDomain());
45+
}
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\ViewModel;
9+
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
13+
class CookieSettings implements ArgumentInterface
14+
{
15+
public const XML_PATH_COOKIE_DOMAIN = 'web/cookie/cookie_domain';
16+
17+
/**
18+
* @var ScopeConfigInterface
19+
*/
20+
private $scopeConfig;
21+
22+
/**
23+
* @param ScopeConfigInterface $scopeConfig
24+
*/
25+
public function __construct(ScopeConfigInterface $scopeConfig)
26+
{
27+
$this->scopeConfig = $scopeConfig;
28+
}
29+
30+
/**
31+
* Get cookie domain for a store view
32+
*
33+
* @return mixed
34+
*/
35+
public function getCookieDomain()
36+
{
37+
return $this->scopeConfig->getValue(
38+
self::XML_PATH_COOKIE_DOMAIN,
39+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
40+
);
41+
}
42+
}

app/code/Magento/Customer/view/frontend/layout/default.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<arguments>
5353
<argument name="auth" xsi:type="object">Magento\Customer\ViewModel\Customer\Auth</argument>
5454
<argument name="json_serializer" xsi:type="object">Magento\Customer\ViewModel\Customer\JsonSerializer</argument>
55+
<argument name="cookie_settings" xsi:type="object">Magento\Customer\ViewModel\CookieSettings</argument>
5556
</arguments>
5657
</block>
5758
<block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"

app/code/Magento/Customer/view/frontend/templates/js/customer-data.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
use Magento\Customer\ViewModel\Customer\Data;
77
use Magento\Framework\App\ObjectManager;
8+
use Magento\Customer\ViewModel\CookieSettings;
89

910
/** @var \Magento\Customer\Block\CustomerData $block */
1011

@@ -16,6 +17,8 @@ $jsonSerializer = $block->getJsonSerializer() ??
1617
ObjectManager::getInstance()->get(JsonSerializer::class);
1718
$customerDataUrl = $block->getCustomerDataUrl('customer/account/updateSession');
1819
$expirableSectionNames = $block->getExpirableSectionNames();
20+
/** @var CookieSettings $cookieSettings */
21+
$cookieSettings = $block->getCookieSettings();
1922
?>
2023
<script type="text/x-magento-init">
2124
{
@@ -27,6 +30,7 @@ $expirableSectionNames = $block->getExpirableSectionNames();
2730
$expirableSectionNames
2831
) ?>,
2932
"cookieLifeTime": "<?= $block->escapeJs($block->getCookieLifeTime()) ?>",
33+
"cookieDomain": "<?= $block->escapeJs($cookieSettings->getCookieDomain()) ?>",
3034
"updateSessionUrl": "<?= $block->escapeJs($customerDataUrl) ?>",
3135
"isLoggedIn": "<?= /* @noEscape */ $auth->isLoggedIn() ?>"
3236
}

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ define([
232232
path: '/',
233233
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000)
234234
});
235+
236+
if (options.cookieDomain) {
237+
$.cookieStorage.setConf({
238+
domain: options.cookieDomain
239+
});
240+
}
241+
235242
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
236243
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
237244
},

0 commit comments

Comments
 (0)