Skip to content

Commit

Permalink
MAGETWO-91330: [B2B] The product total price value on the second webs…
Browse files Browse the repository at this point in the history
…ite is as on the default website

- reverted fix from community PR #11165
- issue #7582 is fixed via plugin on store view switching
  • Loading branch information
viktym committed Jul 31, 2018
1 parent 3e2e613 commit 0fdd197
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/code/Magento/Quote/Model/QuoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected function loadQuote($loadMethod, $loadField, $identifier, array $shared
if ($sharedStoreIds) {
$quote->setSharedStoreIds($sharedStoreIds);
}
$quote->$loadMethod($identifier)->setStoreId($this->storeManager->getStore()->getId());
$quote->setStoreId($this->storeManager->getStore()->getId())->$loadMethod($identifier);
if (!$quote->getId()) {
throw NoSuchEntityException::singleField($loadField, $identifier);
}
Expand Down
69 changes: 69 additions & 0 deletions app/code/Magento/Quote/Plugin/UpdateQuoteStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Plugin;

use Magento\Checkout\Model\Session;
use Magento\Quote\Model\QuoteRepository;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\StoreCookieManagerInterface;

/**
* Updates quote store id.
*/
class UpdateQuoteStore
{
/**
* @var QuoteRepository
*/
private $quoteRepository;

/**
* @var Session
*/
private $checkoutSession;

/**
* @param QuoteRepository $quoteRepository
* @param Session $checkoutSession
*/
public function __construct(
QuoteRepository $quoteRepository,
Session $checkoutSession
) {
$this->quoteRepository = $quoteRepository;
$this->checkoutSession = $checkoutSession;
}

/**
* Update store id in active quote after store view switching.
*
* @param StoreCookieManagerInterface $subject
* @param null $result
* @param StoreInterface $store
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSetStoreCookie(
StoreCookieManagerInterface $subject,
$result,
StoreInterface $store
) {
$storeCodeFromCookie = $subject->getStoreCodeFromCookie();
if (null === $storeCodeFromCookie) {
return;
}

$quote = $this->checkoutSession->getQuote();
if ($quote->getIsActive() && $store->getCode() != $storeCodeFromCookie) {
$quote->setStoreId(
$store->getId()
);
$this->quoteRepository->save($quote);
}
}
}
18 changes: 18 additions & 0 deletions app/code/Magento/Quote/etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Plugin\UpdateQuoteStore">
<arguments>
<argument name="quoteRepository" xsi:type="object">Magento\Quote\Model\QuoteRepository\Proxy</argument>
<argument name="checkoutSession" xsi:type="object">Magento\Checkout\Model\Session\Proxy</argument>
</arguments>
</type>
<type name="Magento\Store\Api\StoreCookieManagerInterface">
<plugin name="update_quote_store_after_switch_store_view" type="Magento\Quote\Plugin\UpdateQuoteStore"/>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Model\Plugin;

use Magento\Checkout\Model\Session;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\FunctionalTestingFramework\ObjectManagerInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\StoreCookieManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\StoreRepository;
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;

/**
* @magentoAppArea frontend
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UpdateQuoteStoreTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var CartRepositoryInterface
*/
private $quoteRepository;

protected function setUp()
{
$this->objectManager = BootstrapHelper::getObjectManager();
$this->quoteRepository = $this->objectManager->create(CartRepositoryInterface::class);
}

/**
* Tests that active quote store id updates after store cookie change.
*
* @magentoDataFixture Magento/Quote/_files/empty_quote.php
* @magentoDataFixture Magento/Store/_files/second_store.php
* @throws \ReflectionException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function testUpdateQuoteStoreAfterChangeStoreCookie()
{
$secondStoreCode = 'fixture_second_store';
$reservedOrderId = 'reserved_order_id';

/** @var StoreManagerInterface $storeManager */
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
$currentStore = $storeManager->getStore();

$quote = $this->getQuote($reservedOrderId);
$this->assertEquals(
$currentStore->getId(),
$quote->getStoreId(),
'Current store id and quote store id are not match'
);

/** @var Session $checkoutSession */
$checkoutSession = $this->objectManager->get(Session::class);
$checkoutSession->setQuoteId($quote->getId());

$storeRepository = $this->objectManager->create(StoreRepository::class);
$secondStore = $storeRepository->get($secondStoreCode);

$storeCookieManager = $this->getStoreCookieManager($currentStore);
$storeCookieManager->setStoreCookie($secondStore);

$updatedQuote = $this->getQuote($reservedOrderId);
$this->assertEquals(
$secondStore->getId(),
$updatedQuote->getStoreId(),
'Active quote store id should be equal second store id'
);
}

/**
* Retrieves quote by reserved order id.
*
* @param string $reservedOrderId
* @return Quote
*/
private function getQuote(string $reservedOrderId): Quote
{
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
->create();

$items = $this->quoteRepository->getList($searchCriteria)->getItems();

return array_pop($items);
}

/**
* Returns instance of StoreCookieManagerInterface with mocked cookieManager dependency.
*
* Mock is needed since integration test framework use own cookie manager with
* behavior that differs from real environment.
*
* @param $currentStore
* @return StoreCookieManagerInterface
* @throws \ReflectionException
*/
private function getStoreCookieManager(StoreInterface $currentStore): StoreCookieManagerInterface
{
/** @var StoreCookieManagerInterface $storeCookieManager */
$storeCookieManager = $this->objectManager->get(StoreCookieManagerInterface::class);
$cookieManagerMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
->disableOriginalConstructor()
->getMock();
$cookieManagerMock->method('getCookie')
->willReturn($currentStore->getCode());

$reflection = new \ReflectionClass($storeCookieManager);
$cookieManager = $reflection->getProperty('cookieManager');
$cookieManager->setAccessible(true);
$cookieManager->setValue($storeCookieManager, $cookieManagerMock);

return $storeCookieManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Quote\Model;

use Magento\Store\Model\StoreRepository;
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
Expand Down Expand Up @@ -50,6 +51,34 @@ protected function setUp()
$this->filterBuilder = $this->objectManager->create(FilterBuilder::class);
}

/**
* Tests that quote saved with custom store id has same store id after getting via repository.
*
* @magentoDataFixture Magento/Sales/_files/quote.php
* @magentoDataFixture Magento/Store/_files/second_store.php
*/
public function testGetQuoteWithCustomStoreId()
{
$secondStoreCode = 'fixture_second_store';
$reservedOrderId = 'test01';

$storeRepository = $this->objectManager->create(StoreRepository::class);
$secondStore = $storeRepository->get($secondStoreCode);

// Set store_id in quote to second store_id
$quote = $this->getQuote($reservedOrderId);
$quote->setStoreId($secondStore->getId());
$this->quoteRepository->save($quote);

$savedQuote = $this->quoteRepository->get($quote->getId());

$this->assertEquals(
$secondStore->getId(),
$savedQuote->getStoreId(),
'Quote store id should be equal with store id value in DB'
);
}

/**
* @magentoDataFixture Magento/Sales/_files/quote.php
*/
Expand Down Expand Up @@ -126,6 +155,24 @@ public function testSaveWithNotExistingCustomerAddress()
);
}

/**
* Returns quote by reserved order id.
*
* @param string $reservedOrderId
* @return CartInterface
*/
private function getQuote(string $reservedOrderId)
{
$searchCriteria = $this->getSearchCriteria($reservedOrderId);
$searchResult = $this->quoteRepository->getList($searchCriteria);
$items = $searchResult->getItems();

/** @var CartInterface $quote */
$quote = array_pop($items);

return $quote;
}

/**
* Get search criteria
*
Expand Down

0 comments on commit 0fdd197

Please sign in to comment.