UpdateQuoteItemStore.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Quote\Plugin;
  8. use Magento\Checkout\Model\Session;
  9. use Magento\Quote\Model\QuoteRepository;
  10. use Magento\Store\Api\Data\StoreInterface;
  11. use Magento\Store\Model\StoreSwitcherInterface;
  12. /**
  13. * Updates quote items store id.
  14. *
  15. * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
  16. */
  17. class UpdateQuoteItemStore
  18. {
  19. /**
  20. * @var QuoteRepository
  21. */
  22. private $quoteRepository;
  23. /**
  24. * @var Session
  25. */
  26. private $checkoutSession;
  27. /**
  28. * @param QuoteRepository $quoteRepository
  29. * @param Session $checkoutSession
  30. */
  31. public function __construct(
  32. QuoteRepository $quoteRepository,
  33. Session $checkoutSession
  34. ) {
  35. $this->quoteRepository = $quoteRepository;
  36. $this->checkoutSession = $checkoutSession;
  37. }
  38. /**
  39. * Update store id in active quote after store view switching.
  40. *
  41. * @param StoreSwitcherInterface $subject
  42. * @param string $result
  43. * @param StoreInterface $fromStore store where we came from
  44. * @param StoreInterface $targetStore store where to go to
  45. * @param string $redirectUrl original url requested for redirect after switching
  46. * @return string url to be redirected after switching
  47. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  48. */
  49. public function afterSwitch(
  50. StoreSwitcherInterface $subject,
  51. $result,
  52. StoreInterface $fromStore,
  53. StoreInterface $targetStore,
  54. string $redirectUrl
  55. ): string {
  56. $quote = $this->checkoutSession->getQuote();
  57. if ($quote->getIsActive()) {
  58. $quote->setStoreId(
  59. $targetStore->getId()
  60. );
  61. $quote->getItemsCollection(false);
  62. $this->quoteRepository->save($quote);
  63. }
  64. return $result;
  65. }
  66. }