GuestCartCheckoutFieldManagement.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Quote;
  6. use Magento\Framework\Api\AttributeInterface;
  7. use Magento\Framework\Exception\CouldNotSaveException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Quote\Model\GuestCart\GuestShippingAddressManagementInterface;
  10. use Temando\Shipping\Api\Data\Checkout\AddressInterface;
  11. use Temando\Shipping\Api\Data\Checkout\AddressInterfaceFactory;
  12. use Temando\Shipping\Api\Quote\GuestCartCheckoutFieldManagementInterface;
  13. use Temando\Shipping\Model\ResourceModel\Repository\AddressRepositoryInterface;
  14. /**
  15. * Manage Checkout Fields
  16. *
  17. * @package Temando\Shipping\Model
  18. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link http://www.temando.com/
  21. */
  22. class GuestCartCheckoutFieldManagement implements GuestCartCheckoutFieldManagementInterface
  23. {
  24. /**
  25. * @var GuestShippingAddressManagementInterface
  26. */
  27. private $addressManagement;
  28. /**
  29. * @var AddressRepositoryInterface
  30. */
  31. private $addressRepository;
  32. /**
  33. * @var AddressInterfaceFactory
  34. */
  35. private $addressFactory;
  36. /**
  37. * GuestCartCheckoutFieldManagement constructor.
  38. * @param GuestShippingAddressManagementInterface $addressManagement
  39. * @param AddressRepositoryInterface $addressRepository
  40. * @param AddressInterfaceFactory $addressFactory
  41. */
  42. public function __construct(
  43. GuestShippingAddressManagementInterface $addressManagement,
  44. AddressRepositoryInterface $addressRepository,
  45. AddressInterfaceFactory $addressFactory
  46. ) {
  47. $this->addressManagement = $addressManagement;
  48. $this->addressRepository = $addressRepository;
  49. $this->addressFactory = $addressFactory;
  50. }
  51. /**
  52. * @param string $cartId
  53. * @param AttributeInterface[] $serviceSelection
  54. * @return void
  55. * @throws CouldNotSaveException
  56. */
  57. public function saveCheckoutFields($cartId, $serviceSelection)
  58. {
  59. try {
  60. $shippingAddress = $this->addressManagement->get($cartId);
  61. } catch (NoSuchEntityException $exception) {
  62. throw new CouldNotSaveException(__('Unable to load shipping address for specified quote.'));
  63. }
  64. try {
  65. $address = $this->addressRepository->getByQuoteAddressId($shippingAddress->getId());
  66. } catch (NoSuchEntityException $e) {
  67. $address = $this->addressFactory->create(['data' => [
  68. AddressInterface::SHIPPING_ADDRESS_ID => $shippingAddress->getId()
  69. ]]);
  70. }
  71. $address->setServiceSelection($serviceSelection);
  72. $this->addressRepository->save($address);
  73. }
  74. }