Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BillingAddressManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Quote\Model;
8 
11 use Psr\Log\LoggerInterface as Logger;
14 
20 {
26  protected $addressValidator;
27 
33  protected $logger;
34 
40  protected $quoteRepository;
41 
45  protected $addressRepository;
46 
50  private $shippingAddressAssignment;
51 
60  public function __construct(
63  Logger $logger,
65  ) {
66  $this->addressValidator = $addressValidator;
67  $this->logger = $logger;
68  $this->quoteRepository = $quoteRepository;
69  $this->addressRepository = $addressRepository;
70  }
71 
76  public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false)
77  {
79  $quote = $this->quoteRepository->getActive($cartId);
80  $quote->removeAddress($quote->getBillingAddress()->getId());
81  $quote->setBillingAddress($address);
82  try {
83  $this->getShippingAddressAssignment()->setAddress($quote, $address, $useForShipping);
84  $quote->setDataChanges(true);
85  $this->quoteRepository->save($quote);
86  } catch (\Exception $e) {
87  $this->logger->critical($e);
88  throw new InputException(__('The address failed to save. Verify the address and try again.'));
89  }
90  return $quote->getBillingAddress()->getId();
91  }
92 
96  public function get($cartId)
97  {
98  $cart = $this->quoteRepository->getActive($cartId);
99  return $cart->getBillingAddress();
100  }
101 
106  private function getShippingAddressAssignment()
107  {
108  if (!$this->shippingAddressAssignment) {
109  $this->shippingAddressAssignment = ObjectManager::getInstance()
110  ->get(\Magento\Quote\Model\ShippingAddressAssignment::class);
111  }
112  return $this->shippingAddressAssignment;
113  }
114 }
__construct(\Magento\Quote\Api\CartRepositoryInterface $quoteRepository, QuoteAddressValidator $addressValidator, Logger $logger, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository)
$quote
__()
Definition: __.php:13
$address
Definition: customer.php:38
$cartId
Definition: quote.php:22
assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping=false)