ShippingMethodUpdater.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Paypal\Helper;
  7. use Magento\Braintree\Gateway\Config\PayPal\Config;
  8. use Magento\Quote\Api\CartRepositoryInterface;
  9. use Magento\Quote\Model\Quote;
  10. /**
  11. * Class ShippingMethodUpdater
  12. */
  13. class ShippingMethodUpdater extends AbstractHelper
  14. {
  15. /**
  16. * @var Config
  17. */
  18. private $config;
  19. /**
  20. * @var CartRepositoryInterface
  21. */
  22. private $quoteRepository;
  23. /**
  24. * Constructor
  25. *
  26. * @param Config $config
  27. * @param CartRepositoryInterface $quoteRepository
  28. */
  29. public function __construct(
  30. Config $config,
  31. CartRepositoryInterface $quoteRepository
  32. ) {
  33. $this->config = $config;
  34. $this->quoteRepository = $quoteRepository;
  35. }
  36. /**
  37. * Execute operation
  38. *
  39. * @param string $shippingMethod
  40. * @param Quote $quote
  41. * @return void
  42. * @throws \InvalidArgumentException
  43. */
  44. public function execute($shippingMethod, Quote $quote)
  45. {
  46. if (empty($shippingMethod)) {
  47. throw new \InvalidArgumentException('The "shippingMethod" field does not exists.');
  48. }
  49. if (!$quote->getIsVirtual()) {
  50. $shippingAddress = $quote->getShippingAddress();
  51. if ($shippingMethod !== $shippingAddress->getShippingMethod()) {
  52. $this->disabledQuoteAddressValidation($quote);
  53. $shippingAddress->setShippingMethod($shippingMethod);
  54. $shippingAddress->setCollectShippingRates(true);
  55. $quote->collectTotals();
  56. $this->quoteRepository->save($quote);
  57. }
  58. }
  59. }
  60. }