Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BillingAddressPersister.php
Go to the documentation of this file.
1 <?php
7 
14 
16 {
20  private $addressValidator;
21 
25  private $addressRepository;
26 
31  public function __construct(
32  QuoteAddressValidator $addressValidator,
33  AddressRepositoryInterface $addressRepository
34  ) {
35  $this->addressValidator = $addressValidator;
36  $this->addressRepository = $addressRepository;
37  }
38 
47  public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false)
48  {
50  $this->addressValidator->validate($address);
51  $customerAddressId = $address->getCustomerAddressId();
52  $shippingAddress = null;
53  $addressData = [];
54 
55  if ($useForShipping) {
57  }
58  $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
59  if ($customerAddressId) {
60  try {
61  $addressData = $this->addressRepository->getById($customerAddressId);
62  } catch (NoSuchEntityException $e) {
63  // do nothing if customer is not found by id
64  }
65  $address = $quote->getBillingAddress()->importCustomerAddressData($addressData);
66  if ($useForShipping) {
67  $shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData);
68  $shippingAddress->setSaveInAddressBook($saveInAddressBook);
69  }
70  } elseif ($quote->getCustomerId()) {
71  $address->setEmail($quote->getCustomerEmail());
72  }
73  $address->setSaveInAddressBook($saveInAddressBook);
74  $quote->setBillingAddress($address);
75  if ($useForShipping) {
76  $shippingAddress->setSameAsBilling(1);
77  $shippingAddress->setCollectShippingRates(true);
78  $quote->setShippingAddress($shippingAddress);
79  }
80  }
81 }
$addressRepository
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$quote
$shippingAddress
Definition: order.php:40
$address
Definition: customer.php:38
$addressData
Definition: order.php:19
__construct(QuoteAddressValidator $addressValidator, AddressRepositoryInterface $addressRepository)